BroadcastReceiver带有AlarmManager和AndroidAnnotations

时间:2016-02-24 00:17:05

标签: broadcastreceiver alarmmanager android-annotations

我努力做到这一点,但我无法弄清楚出了什么问题。如果我拨打sendBroadcast(new Intent(ACTION)),我的广播接收器会被调用,但它不能与AlarmManager一起使用:

@EReceiver
public class MyBroadcastReceiver extends AbstractBroadcastReceiver {

public static final int REQUEST_CODE = 12345;
public static final String ACTION = "com.xxx.yyy.alarm";

public static void setAlarm(Context context) {
    Log.e("AUTOMATIC", "setAlarm");
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, MyBroadcastReceiver.class);
    intent.setAction(ACTION);
    PendingIntent pIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 5000, pIntent);
}

@ReceiverAction(ACTION)
public void init(Context context) {
    Log.e("BROADCAST", "woke up receiver action");
    MyService_.intent(context).run().start();
}
}

2 个答案:

答案 0 :(得分:1)

问题是您在intent中使用的是原始类,而不是生成的类。改为:

Intent intent = new Intent(context, MyBroadcastReceiver_.class);

请注意类名中的下划线。

答案 1 :(得分:0)

您需要在扩展BroadcastReceiver

的类中实现onReceived方法
 @Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        // Set the alarm here.
    }
}

并且不要忘记许可:

并且如果尚未完成,请在清单中添加:

<receiver android:name="com.theClassThatExtendBroadcastReceiver">
    </receiver>

了解更多信息: http://developer.android.com/training/scheduling/alarms.html