How do you start an Activity with AlarmManager in Android?

前端 未结 8 921
鱼传尺愫
鱼传尺愫 2020-12-01 17:01

I\'ve poured through a dozen tutorials and forum answers about this problem, but still haven\'t been able to get some working code together. I\'ll try to keep the question s

8条回答
  •  再見小時候
    2020-12-01 17:16

    In my experience you can achieve this without broadcast receiver, just use PendingIntent.getActivity() instead of getbroadcast()

    private void setReminder(){
    
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Calendar startTime = Calendar.getInstance();
                startTime.add(Calendar.MINUTE, 1);
                Intent intent = new Intent(ReminderActivity.this, ReminderActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent pendingIntent = PendingIntent.getActivity(ReminderActivity.this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                alarmManager.set(AlarmManager.RTC, startTime.getTimeInMillis(), pendingIntent);   
    }
    

    I've tested this code on android O but I'm not sure about other android versions please inform me if this doesn't work on any other android version.

提交回复
热议问题