BroadcastReceiver没有收到任何数据

时间:2016-06-24 02:59:40

标签: android broadcastreceiver android-broadcastreceiver

我使用此代码将数据发送到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);

和BroadcastReceiver代码

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
    }
}

这是我的清单

  <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>

现在在

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

邮件已打印,我看到了,但未发送到BroadcastReceiver,或者BroadcastReceiver是否未收到数据?我不确定问题出在哪里。

2 个答案:

答案 0 :(得分:1)

您可以在发送广播时使用BOOT_COMPLETED ID

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

或在SEND

中指定<intent-filter>操作
<intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" />
         <action android:name="android.intent.action.SEND" />
</intent-filter> 

注意:确保您的BroadcastReceiver不是内部类,否则在其中添加静态或在上层移动它。

答案 1 :(得分:0)

您需要在清单

中为广播接收器注册Intent.ACTION_SEND
 <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>