简体   繁体   中英

BroadcastReceiver's onReceive() not invoked for Alarm

I have been trying to setup an Alarm, with a BroadcastReceiver that is invoked when the alarm is triggered. Spent hours on this, but I am unable to get the onReceive() to be called.

Somehow this works nicely on API 21, but consistently fails on API 18, which is where I want this to work.


dumpsys alarm

RTC_WAKEUP #2: Alarm{b1fc3710 type 0 srk.test.broadcastreceiver}

type=0 when=-12s1ms repeatInterval=0 count=0

operation=PendingIntent{b1afd430: PendingIntentRecord{b1fc7c28 srk.test.broadcastreceiver broadcastIntent}}

manifest

<application ...> 

    <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
        ...
    <receiver
        android:name="srk.test.broadcastreceiver.Receiver"
                android:exported="true" >
            <intent-filter>
                <action android:name="srk.test.broadcastreceiver.alarm_intent" >
                    </action>
            </intent-filter>
    </receiver>
</application>

Activity

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent intent = new Intent("srk.test.broadcastreceiver.alarm_intent");

        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 123456789,
                            intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.cancel(pendingIntent);

        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 1000), pendingIntent);

        Log.i("SRK", "Alarm Scheduled ... ");
        Toast.makeText(getApplicationContext(), "Alarm set", Toast.LENGTH_LONG).show();
    }
});

Receiver

public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Log.i("SRK", "Alarm Triggered!");
    Toast.makeText(context, "Alarm Triggered!", Toast.LENGTH_LONG).show();

}

I am testing on Android Studio on Windows with an Emulator for Nexus 5, API 18.

I hope this level of detail is enough for reproducing this issue. How can I make this work?

Activity

     Button button = (Button) findViewById(R.id.button);
     button.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {

     Intent intent = new Intent(getActivity(), BroadcastReceiver .class);
                    String strAction = "srk.test.broadcastreceiver.alarm_intent";
                    intent.setAction(strAction);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 123456789,
                        intent, 0);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 1000), pendingIntent);

    Log.i("SRK", "Alarm Scheduled ... ");
    Toast.makeText(getApplicationContext(), "Alarm set", Toast.LENGTH_LONG).show();
 }
 });

Reciever

public class Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

if(intent.getAction().equals("srk.test.broadcastreceiver.alarm_intent")){
Log.i("SRK", "Alarm Triggered!");
Toast.makeText(context, "Alarm Triggered!", Toast.LENGTH_LONG).show();

}}}

You need to set intent action as you have added in to your manifest file Try this code it will sure helps you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM