medo
medo

Reputation: 479

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.

Upvotes: 0

Views: 1978

Answers (2)

Enzokie
Enzokie

Reputation: 7415

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.

Upvotes: 1

Dang Cong Cong
Dang Cong Cong

Reputation: 94

You need to register Intent.ACTION_SEND for Broadcast receiver in 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" />
         <action android:name="android.intent.action.SEND" />
     </intent-filter> 
</receiver>

Upvotes: 0

Related Questions