Android 如何在模拟器中测试 BOOT_COMPLETED 广播接收器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11325920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:58:38  来源:igfitidea点击:

How To Test BOOT_COMPLETED Broadcast Receiver In Emulator

androidandroid-emulatorbroadcastreceiver

提问by Chirag

I want to check the BROADCAST RECEIVERwith Action BOOT_COMPLETEDin the emulator.

我想检查BROADCAST RECEIVER与行动BOOT_COMPLETEDemulator

Is there any way to check that broadcast receiver in emulator ? How can i restart emulator to check that receiver ? is there any direct command?

有什么方法可以检查模拟器中的广播接收器吗?如何重新启动模拟器以检查该接收器?有直接命令吗?

Thanks in advance.

提前致谢。

回答by MKJParekh

There is no Power Button in Emulator like Devices have,So

模拟器中没有像设备那样的电源按钮,所以

To stop an emulator instance, just close the emulator's window.

要停止模拟器实例,只需关闭模拟器的窗口。

And To Start/Restartit Start from AVD Manager of Eclipseand Your BroadcastReceiver with BOOT_COMPLETE action will get called for sure

Start/Restart在Eclipse的AVD管理器启动和您与BOOT_COMPLETE行动广播接收器将被调用肯定

You can start AVD another way also, From CMDgo to Tools of AndroidSDKand give this commmand E:\android-sdk-windows\tools>emulator -avd AVDNAMEHERE

您也可以通过另一种方式启动 AVD,从CMD转到 AndroidSDK 的工具并给出此命令E:\android-sdk-windows\tools>emulator -avd AVDNAMEHERE

To Send Broadcast from CMDyou can use this Command.

要从CMD 发送广播,您可以使用此命令。

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n package_name/class_name

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -c android.intent.category.HOME -n package_name/class_name



Read more about Android Emulator : Android Emulatorand Using Emulator

阅读有关 Android 模拟器的更多信息:Android 模拟器使用模拟器

回答by Fred Ondieki

Basically this is what you have to do to Emulate the Boot Complete in Android Emulater while is is running:Assuming you are a WindowsUser.The same applies for Linux

基本上,这就是您在运行时在 Android Emulator 中模拟启动完成所需要做的:假设您是Windows用户。这同样适用于Linux

STEPS:On your Console type

步骤:在您的控制台类型上

  1. adb shell

  2. am broadcast -a android.intent.action.BOOT_COMPLETED

  1. 亚行外壳

  2. 上午广播 -a android.intent.action.BOOT_COMPLETED

That is all folks. see the diagram below for a detailed illustration.

就这些了。有关详细说明,请参见下图

enter image description here

在此处输入图片说明

Happy Coding!

快乐编码!

回答by ejboy

Some apps may misbehave if BOOT_COMPLETED is received twice, instead limit broadcast to your package only:

如果 BOOT_COMPLETED 被接收两次,某些应用程序可能会行为不端,而只限制广播到您的包:

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

回答by Julio Rodrigues

If you're using flavors, be careful with the .component name shortcut notation.

如果您使用的是风味,请注意.组件名称快捷方式符号。

Example

例子

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED -n com.companyname.develop/com.companyname.notification.RescheduleLocalNotificationsAtBootReceiver

回答by peterchaula

On my Lollipop emulator, it required me to run as root:

在我的 Lollipop 模拟器上,它要求我以 root 身份运行:

adb shell su root am broadcast -a android.intent.action.BOOT_COMPLETED

adb shell su root am broadcast -a android.intent.action.BOOT_COMPLETED

Without root, the command fails - alarm manager does not send the broadcast:

没有 root,命令失败 - 警报管理器不发送广播:

adb shell am broadcast -a android.intent.action.BOOT_COMPLETED

Broadcasting: Intent { act=android.intent.action.BOOT_COMPLETED }
java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED from pid=26595, uid=2000
    at android.os.Parcel.readException(Parcel.java:1684)
    at android.os.Parcel.readException(Parcel.java:1637)
    at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:3537)
    at com.android.commands.am.Am.sendBroadcast(Am.java:772)
    at com.android.commands.am.Am.onRun(Am.java:404)
    at com.android.internal.os.BaseCommand.run(BaseCommand.java:51)
    at com.android.commands.am.Am.main(Am.java:121)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:262)

回答by Fabian

To restart the emulator just open your command line and switch to folder where your adb is (if not in system path) and enter:

要重新启动模拟器,只需打开命令行并切换到 adb 所在的文件夹(如果不在系统路径中)并输入:

adb restart

(then you can abort the command with Strg - C to get cmd line back)

(然后您可以使用 Strg - C 中止命令以返回 cmd 行)