简体   繁体   中英

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.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM