簡體   English   中英

如何驗證來自不同應用程序的廣播接收器的 onReceive 方法是否被實際調用?

[英]How can I verify that the onReceive method of a Broadcast Receiver from a different App is actually called?

我是 Android 的初學者,我面臨以下問題。 我正在嘗試從(不同的)應用程序 B 調用應用程序 A 注冊的廣播接收器。在應用程序 A 的廣播接收器中,為了測試目的,我實現了以下onReceive()方法:

@Override
public void onReceive(Context context, Intent intent) {
    //TODO: React to the Intent received.
    System.out.println("onReceive has been called");
}

在 App B 中使用以下代碼調用廣播接收器:

Intent explicitIntent = new Intent();
explicitIntent.setComponent(new ComponentName("<A's package name>", "<fully qualified BR class name>"));
sendBroadcast(explicitIntent);

運行 App B 時,我希望看到onReceive has been called string 出現在 Android Studio 控制台上,但事實並非如此。 是因為該方法實際上沒有被調用(出於某種原因),還是因為期望 App B 控制台可以可視化來自 App A 的System.out.println消息是天真的? 在第二種情況下,是否有另一種簡單的方法可以在 Android Studio 中測試 App A 中的onReceive()方法是否實際被調用?

我沒有你的 API 版本來測試, 但是,如果您以不同的方式注冊接收器,您的設置將起作用:

<receiver android:name=".MyBroadcastReceiver"  android:exported="true"/>

暫無
暫無

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

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