基本的Android警报应用程序,广播接收器的onReceive()方法未被调用

Fre*_*cer 3 java android

我正在制作一个报警应用程序.我一步一步地按照Android AlarmController教程进行了一些细微的修改.出于某种原因,当闹钟响起时,我的广播接收器的onReceive()方法没有被调用.这是代码:

// the callback received when the user "sets" the time in the dialog
private TimePickerDialog.OnTimeSetListener mTimeSetListener =
    new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

         time.set( Calendar.HOUR_OF_DAY, hourOfDay );
         time.set( Calendar.MINUTE, minute );

// Tell user alarm was set
String timeSetTo = "Alarm Set: " + time.get( Calendar.HOUR_OF_DAY ) + ":" + time.get( Calendar.MINUTE ) + " " + time.get( Calendar.AM_PM );
if( toast != null ) 
 toast.cancel();

toast = Toast.makeText( AlarmUI.this, "L" + timeSetTo, Toast.LENGTH_LONG );
toast.show();

// When the alarm goes off we want to send an Intent to our Broadcast Receiver (AlarmAction), 
// so set an Intent between the AlarmUI and the AlarmAction
Intent intent = new Intent( AlarmUI.this, AlarmAction.class );

// Create an IntentSender to have the intent executed as a broadcast later when alarm goes off.
PendingIntent sender = PendingIntent.getBroadcast( AlarmUI.this, 0, intent, 0 );

/** Schedule the alarm */
// To get any service we use getSystemService( Service Name )
AlarmManager alarmManager = ( AlarmManager ) getSystemService( ALARM_SERVICE );
/* Finally, we set the alarm to the desired time! WOOHOO! */
alarmManager.set( AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), sender );
        }
    };
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?先感谢您.

Dav*_*own 5

您是否在清单中指定了AlarmAction?

例如 <receiver android:process=":remote" android:name=".AlarmAction"/>

  • 我用于挂起意图的标志是FLAG_UPDATE_CURRENT,这样如果一个已经存在,它将被覆盖,以确保我一次只处理一个警报.一旦警报被触发,我就设置下一个警报,如果有需要的话.我假设需要远程属性,因此进程将在您的应用程序之外私有启动..但不是100%确定. (2认同)