【问题标题】:Cannot receive broadcast in Activity from LocalBroadcastManager in IntentService无法从 IntentService 中的 LocalBroadcastManager 接收 Activity 中的广播
【发布时间】:2014-12-26 06:10:58
【问题描述】:

在Activity和IntentService之间使用广播是非常简单的代码。 MainActivity 启动 SyncService(这是一个 IntentService),SyncService 广播消息,MainActivity 应该从 SyncService 接收消息(通过使用 BroadcastReceiver)。

但奇怪的是 MainActivity 无法从 SyncService 获得任何消息。不知何故,如果我直接在 MainActivity 中调用 LocalBroadcastManager 广播消息(onCreate() 方法),接收者可以得到消息。

是不是因为初始化LocalBroadcastManager的上下文不同?还是有其他问题?

谢谢!

MainActivity 中的相关代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    IntentFilter statusIntentFilter = new IntentFilter(AppConstants.BROADCAST_ACTION);

    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            statusIntentFilter);

    final Intent intent = new Intent(this, SyncService.class);
    this.startService(intent);
    this.sendMessage();
}

private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
        String message = intent.getStringExtra("message");
        Log.d("receiver", "Got message: " + message);
    }
};

SyncService中的相关代码:

public class SyncService extends IntentService {

    private static final String TAG = "SyncService";

    public SyncService() {
        super("SyncService");
        mBroadcaster = new BroadcastNotifier(this);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d(TAG, "Handle intent");
        mBroadcaster.broadcastIntentWithState(AppConstants.STATE_ACTION_STARTED);

        mBroadcaster.broadcastIntentWithState(AppConstants.STATE_ACTION_COMPLETE);

        Log.d(TAG, "Finish intent");
    }

    private BroadcastNotifier mBroadcaster;
}

BroadcastNotifier 中的相关代码:

private LocalBroadcastManager mBroadcaster;

public BroadcastNotifier(Context context) {

    // Gets an instance of the support library local broadcastmanager
    Log.d(TAG, "Start to create broadcast instance with context: " + context);
    mBroadcaster = LocalBroadcastManager.getInstance(context);

}

public void broadcastIntentWithState(int status) {

   Intent localIntent = new Intent(AppConstants.BROADCAST_ACTION);

   // The Intent contains the custom broadcast action for this app
   //localIntent.setAction();

   // Puts the status into the Intent
   localIntent.putExtra(AppConstants.EXTENDED_DATA_STATUS, status);
   localIntent.addCategory(Intent.CATEGORY_DEFAULT);

   // Broadcasts the Intent
   mBroadcaster.sendBroadcast(localIntent);

}

【问题讨论】:

    标签: android localbroadcastmanager


    【解决方案1】:

    broadcastIntentWithState() 中删除addCategory()。这通常不用于任何类型的广播(系统或本地),并且您的 IntentFilter 未按类别过滤。

    【讨论】:

    • 酷,这就是诀窍。非常感谢!你拯救了我的一天。所以在使用intent广播时,IntentFilter实例和Intent实例应该是同一个category。我修改代码以对 IntentFilter 使用相同的类别,效果很好。
    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多