简体   繁体   中英

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

I'm a beginner with Android and I am confronted with the following problem. I'm trying to call a Broadcast Receiver registered by App A from (a different) App B. In App A's Broadcast Receiver I have implemented the following onReceive() method, for testing purposes:

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

The Boradcast Receiver is called with the following code within App B:

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

When running App B, I expected to see the onReceive has been called string appearing on the Android Studio console, but it doesn't. Is it because the method is in fact not called (for some reason) or because it is naive to expect that App B console can visusalize System.out.println messages from App A? In the second case, is there another simple way to test in Android Studio that the onReceive() method from App A is actually called?

I do not have your API ver to test with, but your setup will work if you register your receiver a bit differently:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM