Alarm Manager is not activating broadcast receiver

2019-05-29 01:34发布

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();

}

}

My 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() is never called.

In My 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.!

3条回答
叛逆
2楼-- · 2019-05-29 02:07

I know that this has been answered already, but just for further reference as I ran into the same issue, make sure that your receiver tags are inside the tags, but not inside any other tag such as activity (that's exactly the issue I had).

Also it helps a great deal to check that your alarm has been successfully registered, to so run: adb shell dumpsys alarm

查看更多
Anthone
3楼-- · 2019-05-29 02:08

You just need to registered your receiver with alarm manager this will call broadcast after every 60 second:

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);

In Manifest:

<receiver
        android:name="myPackage.AlarmReceiver"
        android:enabled="true" >
    </receiver>
查看更多
我命由我不由天
4楼-- · 2019-05-29 02:11

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
查看更多
登录 后发表回答