简体   繁体   中英

AlarmManager/BroadcastReceiver not working

I've been trying to setup my first alarm using AlarmManager and BroadCastReceiver as explained here: http://smartandroidians.blogspot.com.es/2010/04/alarmmanager-and-notification-in.html

My setup:

AndroidManifest.xml:

<receiver android:name="es.radiopodcastellano.player.SleepAlarm" />

My main Activity onCreate (this code actually resides on a subclass, but to simplify I put it there):

@Override
    public void onCreate(Bundle savedInstanceState) {
            // <Stripped code>
    AlarmManager alarm = (AlarmManager) currentContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(this.getApplicationContext(), SleepAlarm.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
    alarm.set(AlarmManager.RTC_WAKEUP,(System.currentTimeMillis() + (5 * 1000)),pendingIntent);
}

SleepAlarm.java:

public class SleepAlarm extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Log.d("RPod_SleepAlarm","Alarm!!");
}

}

Output from "adb shell dumpsys alarm" show this, so it seems the intent is being called:

  es.radiopodcastellano.player
221ms running, 32 wakeups
44 alarms: flg=0x4 cmp=es.radiopodcastellano.player/.SleepAlarm

However, Logcat shows nothing for "RPod_SleepAlarm" tag. What could I be doing wrong?

I found the problem.

The receiver on the manifest was inside another receiver for a widget, and it must be a children of the application. So, if you're having the same behaviour as me, please check that you've set up the AndroidManifest.xml correctly:

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