简体   繁体   English

如何验证来自不同应用程序的广播接收器的 onReceive 方法是否被实际调用?

[英]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.我是 Android 的初学者,我面临以下问题。 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:我正在尝试从(不同的)应用程序 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");
}

The Boradcast Receiver is called with the following code within App B:在 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.运行 App B 时,我希望看到onReceive has been called string 出现在 Android Studio 控制台上,但事实并非如此。 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?是因为该方法实际上没有被调用(出于某种原因),还是因为期望 App B 控制台可以可视化来自 App A 的System.out.println消息是天真的? In the second case, is there another simple way to test in Android Studio that the onReceive() method from App A is actually called?在第二种情况下,是否有另一种简单的方法可以在 Android Studio 中测试 App A 中的onReceive()方法是否实际被调用?

I do not have your API ver to test with,我没有你的 API 版本来测试, but your setup will work if you register your receiver a bit differently:但是,如果您以不同的方式注册接收器,您的设置将起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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