所以我设置一个触发意图的闹钟。我还有一个广播监听器,它有一个intentfilter来捕获这个意图,但由于某种原因它无法正常工作。我不确定问题出在哪里(发送或接收或两者兼而有之)。
发送代码:
Intent reminderIntent = new Intent();
reminderIntent.setAction(INTENT_ACTION_ALARM); //INTENT_ACTION_ALARM is String "intent_myaction_alarm"
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, reminderIntent, 0);
Calendar time = Calendar.getInstance();
time.setTimeInMillis(System.currentTimeMillis());
alarmManager.set(AlarmManager.RTC, time.getTimeInMillis(), pendingIntent);
接收代码:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(INTENT_ACTION_ALARM);
alarmReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// DO SOMETHING
}
};
context.registerReceiver(alarmReceiver, intentFilter);
答案 0 :(得分:2)
确保此行位于Manifest.xml文件中:
<receiver android:name=".<YourReceiverClassName>"android:enabled="true"></receiver>