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

时间:2010-10-30 22:17:15

标签: 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 );
        }
    };

有什么想法吗?提前谢谢。

1 个答案:

答案 0 :(得分:5)

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

E.g。 <receiver android:process=":remote" android:name=".AlarmAction"/>