Here Is The Full Code To Set AN Alarm
public class Alarmset extends Activity implements OnClickListener{
Button set;
TimePicker timepicker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarmset);
set=(Button) findViewById(R.id.set);
set.setOnClickListener(this);
timepicker = (TimePicker)findViewById(R.id.timepicker);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==set)
{
Calendar c = Calendar.getInstance();
int y = c.get(Calendar.YEAR);
int m = c.get(Calendar.MONTH);
int d = c.get(Calendar.DAY_OF_MONTH);
c.set(y,m,d,timepicker.getCurrentHour(),timepicker.getCurrentMinute(),00);
setAlarm(c);
}
private void setAlarm(Calendar targetCal){
Intent intent = new Intent(getBaseContext(), My.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Alarmset.this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
Now Reciever Class
public class MyReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Do Your Stuff here
}
And the Xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.mainmenu.Alarmset"
>
<Button
android:id="@+id/set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/timepicker"
android:layout_below="@+id/timepicker"
android:layout_marginTop="62dp"
android:text="Set"
android:textStyle="bold" />
<TimePicker
android:id="@+id/timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>