报警管理器没有激活广播接收器? [英] Alarm Manager is not activating broadcast receiver?

查看:144
本文介绍了报警管理器没有激活广播接收器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个应用程序的工作inwhich我使用 AlarmManager 调度的事情。我的闹钟设置。但这个报警不会调用广播接收器写入捕捉事件。我寻觅了很多,但我没有发现任何东西,解决了问题。我张贴我的code,请看看,看看,如果我失去了一些东西。

I am working on an application inwhich I am using AlarmManager for scheduling things. My Alarm is set. But this Alarm does not invoke BroadcastReceiver written to catch the event. I have searched a great deal but I have not found anything that solves the issue. I am posting my code, please have a look and see if I am missing something.

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>

我无法找出请问问题出。请大家帮我出了。谢谢!

I am unable to figure out the does the problem lie. Please help me out of it. Thanks.!

推荐答案

在使用广播接收器的一种很好的教程中的 http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html 。从本质上讲,你的接收机不声明它会收到什么样的事件。在清单文件中声明需要的是这样的:

A good tutorial on using Broadcast receivers is given in http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html. In essence, your receiver doesn't declare what events it will receive. The declaration in the Manifest file needs something like:

 <receiver
    android:name="myPackage.AlarmReceiver"
    android:enabled="true" >
    <intent-filter>
            <action android:name="your.company.blah.mybroadcast" />
        </intent-filter>
</receiver>

当你创建的意图,它需要

And when you create the intent, it needs

Intent intent = new Intent();
intent.setAction("your.company.blah.mybroadcast");
// All the other things you want to put in the intent

这篇关于报警管理器没有激活广播接收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆