30

Well, I have searched a lot. People say that I need to close the window of emulator to close it. However, I need to reboot the emulator to catch ACTION_BOOT_COMPLETED by my BroadcastReceiver.

How can I do that?

Steven Byle
  • 12,579
  • 4
  • 41
  • 57

7 Answers7

36

You can use the following command from adb:

adb shell am activity/service/broadcast -a ACTION -c CATEGORY -n NAME

for example:

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

Note that class name and package names need to be as defined in the Manifest.

This will generate the Intent you want , directed only to the component you want (otherwise you system will go crazy with BOOT_COMPLETED sent...)

laalto
  • 137,703
  • 64
  • 254
  • 280
Anat
  • 406
  • 3
  • 5
  • Thanks. The code looks great. Currently I don't have time to test. But I found out that without using snapshot, the emulator is a lot faster in booting, and closing :-) I like it! –  Feb 13 '12 at 08:13
  • 4
    when I execute that from Android Studio terminal (emulator with Android 7.1.1 is running), I get the message `java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED from pid=7432, uid=2000` – Someone Somewhere Jun 26 '18 at 11:56
31

Or another way (also from the command line):

adb shell stop
adb shell start
Booger
  • 18,137
  • 6
  • 51
  • 64
  • 1
    yeah i was able to verify my onReceive was called listening for BOOT_COMPLETED using this way. However, the break point I had didn't hit because the debugger became detached, but I saw my log statement in DDMS. – Adam Johns May 21 '14 at 19:38
  • 5
    in Android studio terminal, I get the message `stop: must be root` whilst Android 7.1.1 emulator is running – Someone Somewhere Jun 26 '18 at 11:53
9

First, make sure that USB Debugging is enabled from within the emulator:

click:

  • the Home icon
  • Menu icon
  • 'Settings'
  • 'Applications'
  • 'Development'
  • make sure that the box next to 'USB debugging' contains a check mark

from a command-line:

adb -e reboot

EDIT:

This definitely doesn't work... very strange. I tested it and could not make the emulator reboot. It just hangs.

DavidDraughn
  • 1,060
  • 1
  • 8
  • 15
  • Excuse me, could you tell me if you run `adb reboot` successfully? I run it for 45 mins, the process `emulator-arm` keeps using 50% of CPU. It's not responding :-( –  Feb 11 '12 at 17:07
8

To emulate a broadcast action, you can connect via adb to the emulator/device and open a shell:

adb shell

Then, you can broadcast the action you want to test:

am broadcast -a android.intent.action.BOOT_COMPLETED

Please note that, in the latest Android versions, broadcasting the *android.intent.action.BOOT_COMPLETED* action will actually reboot your emulator/device.

Paolo Rovelli
  • 8,082
  • 1
  • 52
  • 37
  • 1
    on Android emulator 7.1.1, I get the following message on Terminal: `java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.BOOT_COMPLETED from pid=6832, uid=2000` – Someone Somewhere Jun 26 '18 at 11:49
0

While using tubemate on android tab, I was not successful in downloading video with the error:host interupped etc. I used the following commands:

adb shell stop
adb shell start

and the application started downloading videos.

shadyyx
  • 15,095
  • 5
  • 48
  • 91
susjoy
  • 1
-1

emulators have on/off button - just click it

Konstantin Pribluda
  • 12,042
  • 1
  • 26
  • 34
  • 3
    When I click the power button (and hold for 2 or 3 secs), it acts as like real device: it shows me menu of Silent mode/ Airplane mode/ Power off. I click Power off, but then I can't turn it on :-( . It keeps showing "Shutting down..." –  Feb 11 '12 at 16:15
  • 2
    holding power button helps (current latest emulator version) – Alexander Dyagilev Jun 24 '18 at 14:01
-3

and another way:

If you start your emulator, and select the "wipe user data" checkbox, you will also receive that notification when boot is completed.

Booger
  • 18,137
  • 6
  • 51
  • 64
  • 1
    But if I wipe data, the app that I installed will be erased :-) –  Feb 11 '12 at 16:33
  • Have you tested that Wipe Use Data does in fact erase installed apps? I believe that it will wipe data (preferences, cache, etc) that have been stored by apps, but not the apps themselves... – DavidDraughn Feb 13 '12 at 07:16
  • @DavidDraughn, Sorry for late reply... But it erases all and returns original. –  Mar 27 '12 at 02:09