Broadcast not recieved when called by an intent

Total Post:45

Points:315
 1405  View(s)
Ratings:
Rate this:

I have broadcast which is recieved on system boot as well as by an intent. This broadcast is to set alarm.

Intent intent = new Intent();
intent.setAction("recievers.BroadCastBootRec");
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
getActivity().sendBroadcast(intent);
My Broadcast class :
@Override
public void onReceive(Context context, Intent intent) {
    c = context;
    Log.d("HirakDebug", "BroadCast Recieved");
    getDatesFromDatabase();
    getDateDifference();
    setAlarmI();
}

Manifest.xml

<receiver android:name=".recievers.BroadCastBootRec"
        android:label="BootReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

  1. Post:51

    Points:359
    Re: Broadcast not recieved when called by an intent

    You have to register two actions and to make things clear. android:name is the Class name of the receiver you had written, not the action. So, for the below receiver your receiver class name will be "BroadCastBootRec"

    <receiver android:name=".recievers.BroadCastBootRec"
        android:label="BootReceiver">
    <intent-filter>
        <action android:name="com.example.BroadCastBootRec" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
    </receiver>
    Intent intent = new Intent();
    intent.setAction("com.example.BroadCastBootRec");
    intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
    getActivity().sendBroadcast(intent);

    The android system will inform your receiver when ever an action BOOT_COMPLETED andcom.example.BroadCastBootRec is triggered

    public class BroadCastBootRec extends BroadcastReceiver

      @Override
      public void onReceive(Context context, Intent intent) {
       c = context;
       Log.d("HirakDebug", "BroadCast Recieved");
       getDatesFromDatabase();
       getDateDifference();
       setAlarmI();
    }
    }

      Modified On Apr-06-2018 06:35:26 AM

Answer