w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Why doesn't my BroadcastReceiver's onReceive get called when the BroadcastReceiver is an inner class?

Why is that?

you are got major fundamental mistake about how to send and receive broadcast mechanism works:

you are providing to alarm manager a PendingIntent to start broadcast, but in practice - you providing it intent without any Action specified.

sending broadcast without an action is meaningless!!!

you should add to the intentAlarm object an custom action string that identify your broadcast, and register your AlarmReceiver receiver programatically with an IntentFilter that handles this custom action:

 Intent intentAlarm = new Intent(CUSTOM_ACTION_STRING);
 ....
 ....

(you should hold reference to an instance of your AlarmReceiver class. that's the mAlarmReceiver ...) add to the Activity onResume() callback registration to the broadcast:

 @Override
public void onResume() {
    super.onResume();

    registerReceiver(mAlarmReceiver , new
IntentFilter(CUSTOM_ACTION_STRING));
}

don't forget also to unregister the receiver:

@Override
public void onPause() {
    unregisterReceiver(mAlaramReceiver);

    super.onPause();
}




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.