android - LocalBroadcastManager 未在 Activity 中接收广播

标签 android android-activity service android-context localbroadcastmanager

我正在使用 LocalBroadcastManager 使用 APPLICATION CONTEXT 向我的 Activity 和服务进行广播,如下所示:

public class CommonForApp extends Application{
    public void broadcastUpdateUICommand(String[] updateFlags,
                String[] flagValues) {
            Intent intent = new Intent(UPDATE_UI_BROADCAST);
            for (int i = 0; i < updateFlags.length; i++) {
                intent.putExtra(updateFlags[i], flagValues[i]);
            }

            mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);
            mLocalBroadcastManager.sendBroadcast(intent);

        }}

现在在我的服务中使用监听器,我正在调用 broadcastUpdateUICommand() ,如下所示:

public class mService extends Service {
    public BuildNowPLaylistListListener buildCursorListener = new BuildNowPLaylistListListener() {

            @Override
            public void onServiceListReady() {
    mApp.broadcastUpdateUICommand(
                        new String[] { CommonForApp.INIT_DRAWER},
                        new String[] {""});

            }}}

我正在我的 Activity 中接收广播,如下所示:

public class mActivity extends Activity{
    BroadcastReceiver mReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
           mtoast.showtext("in onreceive"); //toast to check 

                if (intent.hasExtra(CommonForApp.INIT_DRAWER))
                    initialiseDrawer();

    }};


}

mApp 是应用程序的实例。 CommonForApp 是我的应用程序类。

但在我的 Activity 中我没有收到任何广播(广播管理器是使用应用程序上下文初始化的)。

谁能告诉我为什么我的 Activity 收不到广播? ..

.提前致谢!

最佳答案

Activity 中:

protected BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, final Intent intent) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(intent.hasExtra("type")){
                    // Do some action
                }
            }
        });

    }
};

@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
            new IntentFilter("data-loaded"));
}

protected void onDestroy() {
    super.onDestroy();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
}

然后你发送广播:

public static void sendBroadcastMessageDataLoaded(Context context, String dataType){
    Intent intent = new Intent("data-loaded");
    intent.putExtra("type", dataType);
    LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}

关于android - LocalBroadcastManager 未在 Activity 中接收广播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37671055/

相关文章:

带有 Completable 的 Android 房间插入不起作用?使用 @insert 注释的方法可以返回 void,long?

Android 发送带有文本和图像的电子邮件

android - ListFragment 与 ListActivity - 使用哪一个?

android - 将 Activity 与 BottomNavigationView 一起使用

java - 这是内存泄漏吗?

node.js - 使用 'forever' 仍然是将 NodeJS 作为 linux/unix 服务运行的建议方法吗?

android - Flutter 有哪些原生 Android 或 iOS SDK 不能做到的?

Android - String.format - MissingFormatArgumentException 和 InvocationTargetException

JavaFX 服务/任务/ worker 和自动化

android - 如何在运行时设置应用程序的入口 Activity ?