medo medo - 5 months ago 13
Android Question

BroadcastReceiver not receiving any data

I use this code to send data to my

BroadcastReceiver
.

Log.d("recive message message message", message);

Intent resultBroadCastIntent = new Intent();
resultBroadCastIntent.setAction(Intent.ACTION_SEND);
resultBroadCastIntent.addCategory(Intent.CATEGORY_DEFAULT);
resultBroadCastIntent.putExtra(OUTPUT_TEXT, message);

sendBroadcast(resultBroadCastIntent);


and the BroadcastReceiver code

public class Broadcast_Receiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, Notification_Intent_Service.class));
String resultText =intent.getStringExtra(Notification_Intent_Service.OUTPUT_TEXT);

Log.d("recive dwwwwwwwwwwwwwwww", resultText);
// this never run when message arrive
}
}


and this is my manifest

<receiver
android:name=".Service.Broadcast_Receiver"
android:enabled="true"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>


now in

Log.d("recive message message message", message);


the message is printed and I see it but it wasn't sent to the
BroadcastReceiver
or is it that the
BroadcastReceiver
didn't receive the data? I'm not sure where the problem is.

Answer

You can use the BOOT_COMPLETED id when sending a broadcast

Intent resultBroadCastIntent = new Intent("android.intent.action.BOOT_COMPLETED");
resultBroadCastIntent.putExtra(OUTPUT_TEXT, message);
sendBroadcast(resultBroadCastIntent);

or Specify SEND action in <intent-filter>

<intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
         <action android:name="android.intent.action.SEND" />
</intent-filter> 

Note: Make sure your BroadcastReceiver isn't an Inner class otherwise add static into it or move it in upper level.

Comments