简体   繁体   中英

Set an alarm at specific date and time

I am trying to set an alarm in my application. Whenever the alarm is started, an AlertDialog is shown to the user.

I have created two activities:

  • ActivityA is the one in charge of setting the alarm
  • ActivityB is the one in charge of showing the AlertDialog window to the user

ActivityA : setting the alarm

The alarm date is stored in the variable alarmDate of type GregorianCalendar . I created the alarm using the following code (following the answer here ):

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra(ActivityB.ALARM_MESSAGE, message);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmDate.getTimeInMillis(), pendingIntent);

ActivityB : handling the request

public void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    String message = intent.getStringExtra(ALARM_MESSAGE);      
    AlertDialog dialog = new AlertDialog.Builder(this).setMessage(message)
                       .setPositiveButton("OK", new DialogInterface.OnClickListener()...)
                       .create();
    dialog.show();
    finish();
}

The problem

I have encountered a problem: ActivityB is not able to capture the intent. Thus, I think that it is never created and I am missing something on how to start it.

Moreover, I don't think this is the most desirable solution, since I need the alarm to show the AlertDialog window also in case the application is not running.

Any hint on how to solve these problems?

Thank you.

You are using PendingIntent.getService but should getActivity

UPD About AlertDialog, you could use transparent activity and still open AlertDialog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM