如何让 Android BroadcastReceiver 与 AlarmManager 协同工作?
How to make Android BroadcastReceiver work with AlarmManager?
我有以下代码 post 意图:
alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("myLog");
intent.putExtra("message", new Date().toString());
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME,System.currentTimeMillis()+1000,5000,alarmIntent);
以及以下接收者:
public class myReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
logText.setText(logText.getText()+"\n"+message);
}
}
private BroadcastReceiver mMessageReceiver = new myReceiver();
为什么我的 mMessageReceiver
只用 sendBroadcast
触发一次,但没有收到我的 alarmMgr
广播的意图?
编辑: 这也是我的 AndroidManifest.xml
文件的相关部分:
<receiver android:name=".MainApp$myReceiver" >
<intent-filter>
<action android:name="com.example.*.*" />
</intent-filter>
</receiver>
编辑 2: 更多信息:
@Override public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("myLog");
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
myIntentFilter);
}
这会覆盖 MainActivitie
s onResume()
。
嗯,我在这里可能超出了我的理解范围,但这对我有用。所以在这种情况下,我在当前时间之前设置了一个闹钟(比如 10 分钟左右)。
// Calculate the time when it expires.
long wakeupTime = System.currentTimeMillis() + duration;
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, wakeupTime, pendingIntent);
这会设置一个响铃并唤醒我的 BroadcastReceiver (AlarmReceiver)。我希望这会有所帮助,我不会偏离这里。
我有以下代码 post 意图:
alarmMgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("myLog");
intent.putExtra("message", new Date().toString());
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
alarmMgr.setRepeating(AlarmManager.ELAPSED_REALTIME,System.currentTimeMillis()+1000,5000,alarmIntent);
以及以下接收者:
public class myReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
// Extract data included in the Intent
String message = intent.getStringExtra("message");
logText.setText(logText.getText()+"\n"+message);
}
}
private BroadcastReceiver mMessageReceiver = new myReceiver();
为什么我的 mMessageReceiver
只用 sendBroadcast
触发一次,但没有收到我的 alarmMgr
广播的意图?
编辑: 这也是我的 AndroidManifest.xml
文件的相关部分:
<receiver android:name=".MainApp$myReceiver" >
<intent-filter>
<action android:name="com.example.*.*" />
</intent-filter>
</receiver>
编辑 2: 更多信息:
@Override public void onResume() {
super.onResume();
// Register mMessageReceiver to receive messages.
IntentFilter myIntentFilter = new IntentFilter();
myIntentFilter.addAction("myLog");
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
myIntentFilter);
}
这会覆盖 MainActivitie
s onResume()
。
嗯,我在这里可能超出了我的理解范围,但这对我有用。所以在这种情况下,我在当前时间之前设置了一个闹钟(比如 10 分钟左右)。
// Calculate the time when it expires.
long wakeupTime = System.currentTimeMillis() + duration;
Intent myIntent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, wakeupTime, pendingIntent);
这会设置一个响铃并唤醒我的 BroadcastReceiver (AlarmReceiver)。我希望这会有所帮助,我不会偏离这里。