Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid notification in AlarmReceiver
    text
    copied!<p>I want to be able to create an alarm to ring a notification to the user to start the application every 2 minutes. Everything works fine, but the notification appears when i start my application manually. Here are my code : </p> <p><em>Receiver:</em></p> <pre><code>public void onReceive(Context context, Intent intent) { nm = (NotificationManager) context .getSystemService(Context.NOTIFICATION_SERVICE); CharSequence from = "myactivity"; CharSequence message = "click to start activity"; Intent scheduledIntent = new Intent(context, AmslerTestActivity.class); PendingIntent contentIntent = PendingIntent.getActivity(context, 0, scheduledIntent, 0); Notification notif = new Notification(R.drawable.icon, message, System.currentTimeMillis()); notif.setLatestEventInfo(context, from, message, contentIntent); notif.flags = Notification.FLAG_AUTO_CANCEL; notif.defaults |= Notification.DEFAULT_SOUND; nm.notify(1, notif); scheduledIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(scheduledIntent); } </code></pre> <p><em>Scheduler:</em></p> <pre><code>cal.set(Calendar.HOUR_OF_DAY, 12); cal.set(Calendar.MINUTE, 01); cal.set(Calendar.SECOND, 0); Intent intent = new Intent(myactivity.this, AlarmReceiver.class); PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); // Get the AlarmManager service am = (AlarmManager) getSystemService(Context.ALARM_SERVICE); am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 2 * 60 * 1000, sender); </code></pre> <p>I want the notification to only ring when the code is not running. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload