本文介绍了在活动中IntentService不能接收广播从LocalBroadcastManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是很简单的code到活动和IntentService之间使用广播。在MainActivity开始SyncService(这是一个IntentService),所述SyncService广播消息和MainActivity应(通过使用广播接收器)接收来自SyncService消息

it is very simple code to use broadcast between Activity and IntentService. The MainActivity starts SyncService (which is an IntentService), the SyncService broadcasts messages and MainActivity should receive the messages from SyncService (by using BroadcastReceiver).

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

but it is strange that the MainActivity cannot get any message from SyncService. Somehow if I call LocalBroadcastManager to broadcast message directly in MainActivity (onCreate() method), the receiver can get the message.

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

is it because of the different context to initialize LocalBroadcastManager? or there is any other problem?

谢谢!

初​​步认识code在MainActivity:

Relavant code in 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);
    }
};

相关code在SyncService:

Relevant code in 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;
}

Relavent code在BroadcastNotifier:

Relavent code in 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);

}

推荐答案

删除 addCategory() broadcastIntentWithState()。通常不与任何形式的广播(系统或本地)使用,而你的的IntentFilter 的类别没有过滤。

Remove addCategory() from broadcastIntentWithState(). That is not normally used with any sort of broadcasts (system or local), and your IntentFilter is not filtering on category.

这篇关于在活动中IntentService不能接收广播从LocalBroadcastManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 08:29