我正在处理一个应用程序,我正在使用AlarmManager
来安排事情。我的闹钟设定好了。但是,此警报不会调用BroadcastReceiver
来捕获事件。我搜索了很多,但我没有找到任何解决问题的方法。我发布了我的代码,请看看我是否遗漏了一些东西。
AlarmManagerClass:
public class ScheduleMessageManager {
Context context;
PendingIntent sender;
AlarmManager am;
public ScheduleMessageManager(Context context) {
this.context = context;
}
public void addAlram(int scheduledMessageID, long scheduledTime) {
// Activate Broadcast Receiver to receive broadcasts
activateBroadcastReceiver();
//Calendar cal = Calendar.getInstance();
Intent intent = new Intent(context, AlarmReceiver.class);
// In reality, you would want to have a unique variable for the request
// code
intent.putExtra("scheduledMessageID", scheduledMessageID);
sender = PendingIntent.getBroadcast(context, scheduledMessageID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
am = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, scheduledTime, sender);
Log.e("In ScheduleMessageManage", "***** Alarm is set to the mmessage *****");
}
public void cancelPeriodicSchedule(PendingIntent sender) {
if (am != null) {
if (sender != null) {
am.cancel(sender);
sender.cancel();
}
}
// Deactivate Broadcast Receiver to stop receiving broadcasts
deactivateBroadcastreceiver();
}
private void activateBroadcastReceiver() {
PackageManager pm = context.getPackageManager();
ComponentName componentName = new ComponentName(context, AlarmReceiver.class);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
Toast.makeText(context, "activated", Toast.LENGTH_LONG).show();
}
private void deactivateBroadcastreceiver() {
// TODO Auto-generated method stub
PackageManager pm = context.getPackageManager();
ComponentName componentName = new ComponentName(context, AlarmReceiver.class);
pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Toast.makeText(context, "cancelled", Toast.LENGTH_LONG).show();
}
}
我的AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
int pendingIntentID; // same as scheduledMessageID
@Override
public void onReceive(Context context, Intent intent) {
Log.e("In On Receive", "Alarm has Initiated Broadcast Receiver....");
if (intent.hasExtra("scheduledMessageID")) {
pendingIntentID = intent.getExtras().getInt("scheduledMessageID");
Intent sendMessageServiceIntent = new Intent(context, SendMessageService.class);
sendMessageServiceIntent.putExtra("pendingIntentID", pendingIntentID);
context.startService(sendMessageServiceIntent);
}
}
}
OnReceieve()
永远不会被调用。
在我的 Manifest.xml
中 <receiver
android:name="myPackage.AlarmReceiver"
android:enabled="true" >
</receiver>
我无法弄清楚问题所在。请帮助我。谢谢!
答案 0 :(得分:4)
http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html中提供了有关使用广播接收器的优秀教程。从本质上讲,您的接收器不会声明它将接收哪些事件。清单文件中的声明需要类似:
<receiver
android:name="myPackage.AlarmReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="your.company.blah.mybroadcast" />
</intent-filter>
</receiver>
当你创建意图时,它需要
Intent intent = new Intent();
intent.setAction("your.company.blah.mybroadcast");
// All the other things you want to put in the intent
答案 1 :(得分:2)
我知道这已经得到了解答,但是为了进一步参考,我遇到了同样的问题,请确保你的接收器标签在标签内,但不在任何其他标签内,例如活动(&#39;正是我的问题。
同样有助于检查您的闹钟是否已成功注册,以便运行: adb shell dumpsys alarm
答案 2 :(得分:-1)
您只需要在闹钟管理器中注册您的接收器,这将在每60秒后调用一次广播:
Intent broadcastIntent = new Intent(YourActivity.this, AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, broadcastIntent, 0);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
long timeInterval = 60 * 1000;
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), timeInterval, pi);
在清单中:
<receiver
android:name="myPackage.AlarmReceiver"
android:enabled="true" >
</receiver>