25

I need to start the activity AlarmReceiver after 10 seconds (for example). I need it to be activated without running the app. But whether the app runs or not the AlarmReceiver do not get called. Any suggestions?

Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 111, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

//alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() 
                                          //+ (10 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
dinesh707
  • 12,106
  • 22
  • 84
  • 134

4 Answers4

44
public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
          String message = "Hellooo, alrm worked ----";
          Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
          Intent intent2 = new Intent(context, TripNotification.class); 
          intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(intent2);
    }

    public void setAlarm(Context context){
        Log.d("Carbon","Alrm SET !!");

        // get a Calendar object with current time
         Calendar cal = Calendar.getInstance();
         // add 30 seconds to the calendar object
         cal.add(Calendar.SECOND, 30);
         Intent intent = new Intent(context, AlarmReceiver.class);
         PendingIntent sender = PendingIntent.getBroadcast(context, 192837, intent, PendingIntent.FLAG_UPDATE_CURRENT);

         // Get the AlarmManager service
         AlarmManager am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
         am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
    }
}

This is the final code i managed to get working. You need to add

 <receiver  android:process=":remote" android:name="AlarmReceiver"></receiver>

just above the </application> tag in Manifest file.

This will set an alarm to trigger in 30 seconds after calling the method SetAlarm()

Community
  • 1
  • 1
dinesh707
  • 12,106
  • 22
  • 84
  • 134
  • 2
    @Alex @dinesh707 Where `SetAlarm()` is called? – Neo Mar 07 '14 at 17:59
  • 10
    Correction: The receiver part must be inside the application-tag! – James Cameron Dec 09 '14 at 16:30
  • The portion in the '' element that goes in the manifest where it says 'android:process=":remote" is what made my troubles go away. I already had the element as well as the 'android:name' inside of it, but it seems it needed the 'android:process=":remote"' to get it over the top. – Evan Sevy Feb 02 '17 at 02:52
  • Oh, sorry. Actually it didn't change anything. I just wasn't waiting long enough for the API to make its calls or whatever. It seems to take about 10-30 seconds before my notification appears in the Notification pane (I was using an AlarmManager to facilitate this). – Evan Sevy Feb 02 '17 at 03:38
  • 1
    I found on an Android 10 and Android 12 phone that the alarm receiver never fired, and I had to remove android:process=":remote" for it to behave as documented. – mcfisty Dec 26 '22 at 04:38
6

As of now, it's not possible to start Alarm without running the app, you must once run your respective app to activate your alarm.. For this....!!

In Your ALARM_ACTIVITY :

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(ALARM_ACTIVITY.this,ALARM_RECEIVER.class); 

PendingIntent pendingIntent = PendingIntent.getBroadcast(SetReminder.this, ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);

alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeInMillis() + 1000, pendingIntent);

In Your ALARM_RECEIVER :

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

notification = new Notification(R.drawable.alarmicon, "charSequence", System.currentTimeMillis());

notification.setLatestEventInfo(context, "alarmTitle", "charSequence", pendingIntent);

notification.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(1, notification);
EdChum
  • 376,765
  • 198
  • 813
  • 562
Prat
  • 143
  • 3
  • 17
5

And if it is still not working getting rid of the android:process=":remote" part may help. Worked for me :)

kellogs
  • 2,837
  • 3
  • 38
  • 51
-1

Also, In addition to the above,I think the methods in the AlarmActivity should be in the oncreate method of the LAUNCHER activity.. In this case, the Alarm Activvity should be the LAUNCHER activity of the app. this solved my problem

Moses Kirathe
  • 322
  • 1
  • 3
  • 14