[英]BroadcastReceiver's onReceive() not invoked for Alarm
I have been trying to setup an Alarm, with a BroadcastReceiver that is invoked when the alarm is triggered. 我一直在尝试设置警报,并使用在触发警报时调用的BroadcastReceiver。 Spent hours on this, but I am unable to get the onReceive() to be called.
花了几个小时,但是我无法调用onReceive()。
Somehow this works nicely on API 21, but consistently fails on API 18, which is where I want this to work. 在某种程度上,这在API 21上可以很好地工作,但是在API 18上却一直失败,这就是我希望它可以在其中工作的地方。
dumpsys alarm dumpsys警报
RTC_WAKEUP #2: Alarm{b1fc3710 type 0 srk.test.broadcastreceiver}
type=0 when=-12s1ms repeatInterval=0 count=0
operation=PendingIntent{b1afd430: PendingIntentRecord{b1fc7c28 srk.test.broadcastreceiver broadcastIntent}}
manifest 表现
<application ...>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
...
<receiver
android:name="srk.test.broadcastreceiver.Receiver"
android:exported="true" >
<intent-filter>
<action android:name="srk.test.broadcastreceiver.alarm_intent" >
</action>
</intent-filter>
</receiver>
</application>
Activity 活动
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("srk.test.broadcastreceiver.alarm_intent");
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 123456789,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 1000), pendingIntent);
Log.i("SRK", "Alarm Scheduled ... ");
Toast.makeText(getApplicationContext(), "Alarm set", Toast.LENGTH_LONG).show();
}
});
Receiver 接收器
public class Receiver extends BroadcastReceiver { 公共类Receiver扩展了BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("SRK", "Alarm Triggered!");
Toast.makeText(context, "Alarm Triggered!", Toast.LENGTH_LONG).show();
}
I am testing on Android Studio on Windows with an Emulator for Nexus 5, API 18. 我正在Windows的Android Studio上针对Nexus 5,API 18的仿真器进行测试。
I hope this level of detail is enough for reproducing this issue. 我希望这种详细程度足以重现此问题。 How can I make this work?
我该如何进行这项工作?
Activity 活动
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), BroadcastReceiver .class);
String strAction = "srk.test.broadcastreceiver.alarm_intent";
intent.setAction(strAction);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 123456789,
intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 1000), pendingIntent);
Log.i("SRK", "Alarm Scheduled ... ");
Toast.makeText(getApplicationContext(), "Alarm set", Toast.LENGTH_LONG).show();
}
});
Reciever Reciever
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("srk.test.broadcastreceiver.alarm_intent")){
Log.i("SRK", "Alarm Triggered!");
Toast.makeText(context, "Alarm Triggered!", Toast.LENGTH_LONG).show();
}}}
You need to set intent action as you have added in to your manifest file Try this code it will sure helps you 添加到清单文件后,您需要设置意图操作尝试此代码,它将确保对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.