带有 AlarmManager 和 AndroidAnnotations 的 BroadcastReceiver
BroadcastReceiver with AlarmManager and AndroidAnnotations
我做了很多尝试来完成这项工作,但我无法弄清楚哪里出了问题。如果我调用 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();
}
}
您需要在扩展 BroadcastReceiver
的 class 中实现 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
问题是您在意图中使用的是原始 class,而不是生成的意图。更改为:
Intent intent = new Intent(context, MyBroadcastReceiver_.class);
注意 class 名称中的下划线。
我做了很多尝试来完成这项工作,但我无法弄清楚哪里出了问题。如果我调用 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();
}
}
您需要在扩展 BroadcastReceiver
的 class 中实现 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
问题是您在意图中使用的是原始 class,而不是生成的意图。更改为:
Intent intent = new Intent(context, MyBroadcastReceiver_.class);
注意 class 名称中的下划线。