2

我正在开发一个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>

我无法弄清楚问题出在哪里。请帮我摆脱它。谢谢。!

4

3 回答 3

3

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 = new Intent();
intent.setAction("your.company.blah.mybroadcast");
// All the other things you want to put in the intent
于 2013-03-07T09:51:59.860 回答
2

我知道这已经得到回答,但只是为了进一步参考,因为我遇到了同样的问题,请确保您的接收器标签在标签内,但不在任何其他标签内,例如活动(这正是我遇到的问题) .

检查您的警报是否已成功注册也很有帮助,因此运行: adb shell dumpsys alarm

于 2015-04-24T12:07:31.663 回答
-2

您只需向警报管理器注册您的接收器,这将在每 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>
于 2017-07-12T06:26:29.670 回答