簡體   English   中英

為什么這個簡單的廣播接收器不起作用?

[英]Why isn't this simple broadcast receiver working?

我有兩個應用程序,每個應用程序都有一個無障礙服務。 一個應用程序的無障礙服務發送廣播,另一個應用程序的無障礙服務接收它。

這是發送廣播的服務:

    Intent intent = new Intent();
    intent.setAction("com.corps.mypackage");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this is needed if broadcast not being sent from activity
    sendBroadcast(intent);

這是接收廣播的服務:

    IntentFilter intentFilter = new IntentFilter("com.corps.mypackage");
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Do some things
        }
    };
    registerReceiver(receiver, intentFilter);

未接收到廣播。 可能是什么原因?

根據Android 文檔

只要注冊上下文有效,上下文注冊的接收者就會接收廣播。 例如,如果您在 Activity 上下文中注冊,只要該 Activity 未被銷毀,您就會收到廣播。 如果您在應用程序上下文中注冊,只要應用程序正在運行,您就會收到廣播。

您接收應用的上下文可能無效。 考慮改用清單聲明的接收器

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM