How to set Alarm in Android programmatically?
1.add this in manifest file
<receiver android:name=".MyAlarmReciever">
<intent-filter>
<action android:name="com.example.helloandroid.alarms" />
</intent-filter>
</receiver>
make a activity
public class AlrmActivity extends BaseAcitivity { TimePicker myTimePicker; Button buttonstartSetDialog; TextView textAlarmPrompt; TimePickerDialog timePickerDialog; final static int RQS_1 = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_alrm); textAlarmPrompt = (TextView)findViewById(R.id.alarmprompt); buttonstartSetDialog = (Button)findViewById(R.id.startSetDialog); buttonstartSetDialog.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { textAlarmPrompt.setText(""); openTimePickerDialog(false); }}); } private void openTimePickerDialog(boolean is24r){ Calendar calendar = Calendar.getInstance(); timePickerDialog = new TimePickerDialog( AlrmActivity.this, onTimeSetListener, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), is24r); timePickerDialog.setTitle("Set Alarm Time"); timePickerDialog.show(); } TimePickerDialog.OnTimeSetListener onTimeSetListener = new TimePickerDialog.OnTimeSetListener(){ @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { Calendar calNow = Calendar.getInstance(); Calendar calSet = (Calendar) calNow.clone(); calSet.set(Calendar.HOUR_OF_DAY, hourOfDay); calSet.set(Calendar.MINUTE, minute); calSet.set(Calendar.SECOND, 0); calSet.set(Calendar.MILLISECOND, 0); if(calSet.compareTo(calNow) <= 0){ //Today Set time passed, count to tomorrow calSet.add(Calendar.DATE, 1); } setAlarm(calSet); }}; private void setAlarm(Calendar targetCal){ textAlarmPrompt.setText( "\n\n***\n" + "Alarm is set@ " + targetCal.getTime() + "\n" + "***\n"); Intent intent = new Intent(getBaseContext(), MyAlarmReciever.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0); AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent); } }
3 . xml file include
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello" />
<Button
android:id="@+id/startSetDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Target Time"/>
<TextView
android:id="@+id/alarmprompt"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
4 .make alarm manager class
public class MyAlarmReciever extends BroadcastReceiver {
Vibrator v;
Context ct;
String title;
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
ct=context;
Log.e("onReceive", "ladskjflsakjdflskjdflskjdfslkjdflasdf");
Toast.makeText(context, "OnReceive alarm test", Toast.LENGTH_SHORT).show();
v=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(3000);
int badgeCount = 1;
// ShortcutBadger.applyCount(context, badgeCount);
ShortcutBadger.removeCount(context);
Bundle bundle = intent.getExtras();
try{
title = intent.getExtras().get("title").toString();
// title = intent.getStringExtra("title");
Toast.makeText(context, title, Toast.LENGTH_LONG).show();
}catch(Exception e){
e.printStackTrace();
}
// if (!Utlis.checkNetworkConnection(context)) {
//
// Notification(context, "Wifi Connection off");
//
// } else {
Notification(context, "Please pray for this prayer ");
// }
}
public void Notification(Context context, String message) {
// Set Notification Title
String strtitle = "iPray Prayer Reminder";
// Open NotificationView Class on Notification Click
Intent intent = new Intent(context, PrayerForOtherActivity.class);
// Send data to NotificationView Class
intent.putExtra("title", title);
intent.putExtra("text", title);
// Open NotificationView.java Activity
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// NotificationCompat.Action action = new NotificationCompat.Action.Builder(R.drawable.app_icon, "Previous", pIntent).build();
// Create Notification using NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context)
// Set Icon
.setSmallIcon(R.drawable.app_icon)
// Set Ticker Message
.setTicker(message)
// Set Title
.setContentTitle(context.getString(R.string.app_name))
// Set Text
.setContentText(message)
// Add an Action Button below Notification
// Set PendingIntent into Notification
.setContentIntent(pIntent)
// Dismiss Notification
.setAutoCancel(true);
// Create Notification Manager
NotificationManager notificationmanager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// Build Notification with Notification Manager
notificationmanager.notify(38, builder.build());
}
}
I think you want to set the alarm for 26th June and not 26th July. If so then change cal.set(Calendar.MONTH,6);
to cal.set(Calendar.MONTH,5);
because the months are zero-based. if you intend the alarm to fire on 26th july then it is expected that the alarm will fire when the date-time is 26th July 2011, 17:30