简体   繁体   English

AlarmManager / BroadcastReceiver无法正常工作

[英]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 我一直在尝试使用AlarmManager和BroadCastReceiver设置我的第一个警报,如下所述: http ://smartandroidians.blogspot.com.es/2010/04/alarmmanager-and-notification-in.html

My setup: 我的设置:

AndroidManifest.xml: 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): 我的主要Activity onCreate(这段代码实际上驻留在一个子类上,但为了简化我把它放在那里):

@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: 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: “adb shell dumpsys alarm”的输出显示了这一点,因此似乎调用了intent:

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

However, Logcat shows nothing for "RPod_SleepAlarm" tag. 但是,Logcat没有显示“RPod_SleepAlarm”标记。 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: 因此,如果您的行为与我相同,请检查您是否正确设置了AndroidManifest.xml:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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