作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在开发一个应用程序,其中我使用 AlarmManager
来安排事情。我的闹钟已设置。但是此警报不会调用为捕获事件而编写的 BroadcastReceiver
。我进行了大量搜索,但没有找到任何可以解决问题的方法。我正在发布我的代码,请看看我是否遗漏了什么。
报警管理器类:
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();
}
我的报警接收器:
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>
我无法弄清楚问题出在哪里。请帮我摆脱它。谢谢!
最佳答案
http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html 中提供了一个很好的使用广播接收器的教程。 .本质上,您的接收器不会声明它将接收什么事件。 Manifest 文件中的声明需要如下内容:
<receiver
android:name="myPackage.AlarmReceiver"
android:enabled="true" >
<intent-filter>
<action android:name="your.company.blah.mybroadcast" />
</intent-filter>
</receiver>
当您创建 Intent 时,它需要
Intent intent = new Intent();
intent.setAction("your.company.blah.mybroadcast");
// All the other things you want to put in the intent
关于android - 报警管理器没有激活广播接收器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15267483/
我是一名优秀的程序员,十分优秀!