【问题标题】:How To Test BOOT_COMPLETED Broadcast Receiver如何测试 BOOT_COMPLETED 广播接收器
【发布时间】:2017-03-22 10:59:27
【问题描述】:

我想在模拟器中使用 Action BOOT_COMPLETED 检查 BROADCAST RECEIVER。

这是我的代码

public class AutoRunService extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Toast.makeText(UApplication.getInstance(), "Application is ready to open ", Toast.LENGTH_SHORT).show();

        myFunciton(context);

    }
}

public void myFunciton(Context context) {

}

}

<receiver
        android:name=".AutoRunService"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

我正在使用 Windows 10 和 genymotion 模拟器。 有什么方法可以检查模拟器中的广播接收器吗?如何重新启动模拟器以检查该接收器?有没有直接的命令?

提前致谢。

【问题讨论】:

    标签: android android-emulator genymotion android-broadcastreceiver reboot


    【解决方案1】:

    转到 adb 工具 -> 转到 adb shell 并使用以下命令

    adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -p com.example.package
    

    【讨论】:

    • 每个套接字地址(协议/网络地址/端口)通常只允许使用一次。 (10048) 无法从 ADB 服务器读取 ok * 无法启动守护进程 * 错误:无法连接到守护进程
    • 用普通模拟器试试
    • 什么意思? @apk
    • 使用 avd 模拟器 n 检查其他设备上的尝试
    【解决方案2】:

    只需在 onRecieve() 回调中添加调试日志,如下所示:

            public class AutoRunService extends BroadcastReceiver {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                 Log.d("Boot", "completed"); // log to make sure that boot completed action is received
                }
            }
    

    要重启模拟器,请参考以下答案:

    How to reboot emulator to test ACTION_BOOT_COMPLETED?

    【讨论】:

      猜你喜欢
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多