When creating an AlarmManager inside the MainActivity everything works fine. At the specified time, another activation is launched:

 AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Intent intent = new Intent(MainActivity.this, AlarmActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, iterator, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.set(AlarmManager.RTC_WAKEUP, key, pendingIntent); 

But if you do the same not from the MainActivity , but, for example, from the same class AlarmActivity , then upon reaching the specified time, nothing happens.

Intent intent = new Intent ( this , AlarmActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity ( this , ...);

The problem is most likely in the context , but what should it be if the intent is not created from the MainActivity ? Tried it through this and via AlarmActivity.this , and via getApplicationContext()

UPDATE

On alarmActivity there is a button "Postpone", when clicked, the alarm is updated and the activity closes

 public void hold(View view) { long hold = 0; long currentTime = new Date().getTime(); if (spinner.getSelectedItemPosition() == 0) //hold = 5 * 60 * 1000; hold = 5000; //for test else if (spinner.getSelectedItemPosition() == 1) hold = 10 * 60 * 1000; else if (spinner.getSelectedItemPosition() == 2) hold = 15 * 60 * 1000; else if (spinner.getSelectedItemPosition() == 3) hold = 30 * 60 * 1000; else if (spinner.getSelectedItemPosition() == 4) hold = 60 * 60 * 1000; currentTime += hold; AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Intent intent = new Intent(this, AlarmActivity.class); PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.set(AlarmManager.RTC_WAKEUP, currentTime, pendingIntent); this.finish(); } 

    1 answer 1

    Answered at stackoverflow.com , everything worked out, it works as it should.

    In general, it was necessary to create a class and inherit it from BroadcastReceiver, implement the onReceive method:

     public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent activityIntent = new Intent(context, AlarmActivity.class); int requestCode = intent.getExtras().getInt("intRequest"); String alarmContent = intent.getExtras().getString("keyContent"); Bundle bundle = new Bundle(); bundle.putInt("intRequest", requestCode); bundle.putString("keyContent", alarmContent); activityIntent.putExtras(bundle); activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(activityIntent); } } 

    Next you need to add in the manifest

     <receiver android:name=".AlarmReceiver" > </receiver> 

    Well, then, in my class method AlarmActivity, you can already create an AlarmManager

     public void hold(View view) { long hold = 0; long currentTime = new Date().getTime(); //System.currentTimeMillis() if (spinner.getSelectedItemPosition() == 0) //hold = 5 * 60 * 1000; hold = 5000; else if (spinner.getSelectedItemPosition() == 1) hold = 10 * 60 * 1000; else if (spinner.getSelectedItemPosition() == 2) hold = 15 * 60 * 1000; else if (spinner.getSelectedItemPosition() == 3) hold = 30 * 60 * 1000; else if (spinner.getSelectedItemPosition() == 4) hold = 60 * 60 * 1000; currentTime += hold; AlarmManager alarmManager = (AlarmManager) AlarmActivity.this.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(AlarmActivity.this, AlarmReceiver.class); Bundle bundle = new Bundle(); bundle.putInt("intRequest", requestCode); bundle.putString("keyContent", String.valueOf(textViewAlarmContent.getText())); intent.putExtras(bundle); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.set(AlarmManager.RTC_WAKEUP, currentTime, pendingIntent); this.finish(); }