Home > DeveloperSection > Forums > Broadcast not recieved when called by an intent
Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    November-17-2014 3:19 AM

 Android Android Activity 
Ratings:


 1 Reply(s)
 692  View(s)
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>



Hugh Jackman
Hugh Jackman

Total Post:52

Points:366
Posted on    November-17-2014 6:16 AM

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

}

}


Don't want to miss updates? Please click the below button!

Follow MindStick