如何在Android模拟器中模拟重启,完成BOOT_COMPLETED与持久化任务测试?
1. Simulating Reboot to Test BOOT_COMPLETED Action
To test the android.intent.action.BOOT_COMPLETED intent in an Android emulator, you have two reliable approaches:
Method 1: Full Emulator Reboot (Most Accurate)
This mimics a real device restart perfectly:
- Open your terminal or command prompt and run:
adb reboot
The emulator will shut down and restart, triggering the BOOT_COMPLETED broadcast automatically once the system finishes booting.
Method 2: Send Broadcast Directly (Skip Reboot)
If you want to avoid waiting for a full reboot, you can manually send the broadcast via adb:
adb shell am broadcast -a android.intent.action.BOOT_COMPLETED --user 0
The --user 0 flag ensures the broadcast targets the primary user profile, which is required for most apps to receive it.
2. Verifying Persisted JobInfo After Reboot
Your JobInfo setup with setPersisted(true) is configured correctly to survive reboots—here’s how to confirm it works:
Step-by-Step Check:
- Confirm Job Scheduling: First, make sure you’re actually scheduling the job with
JobScheduler:val jobScheduler = mContext.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler val result = jobScheduler.schedule(jobInfo) // Verify scheduling succeeded with result == JobScheduler.RESULT_SUCCESS - Reboot the Emulator: Use
adb rebootas described earlier. - Check Job Status via ADB: After reboot, run this command to see if your job is still registered:
Look for your job ID (fromadb shell dumpsys jobscheduler | grep "info.dodata.mirror"R.integer.JobID) in the output—if it’s present, the job persisted correctly. - Add Logs for Execution Confirmation: In your
RestoreService, add a log in theonStartJobmethod:
Check Logcat for this message after reboot to confirm the job executed.override fun onStartJob(params: JobParameters?): Boolean { Log.d("RestoreService", "Triggered: Job ran after reboot!") // Your job logic here return false // Return true if handling async work }
3. Correct ADB Broadcast Command for BootReceiver
Looking at your AndroidManifest.xml, Code B is the correct command, but there’s a critical missing piece:
Why Code B Works:
Your BootReceiver is declared as bll.BootReceiver in the info.dodata.mirror package. The -n flag in the adb command requires the component format package/class, which Code B matches exactly (info.dodata.mirror/bll.BootReceiver).
Why Code A Fails:
Code A targets info.dodata.mirror/ui.UIApp, which is your application class—not the broadcast receiver. Broadcasts can’t be delivered directly to an app class unless it’s explicitly registered as a receiver (which it isn’t in your manifest).
Critical Permission Fix:
Even with the correct command, your receiver won’t get the BOOT_COMPLETED broadcast unless you add this permission to your manifest (outside the <application> tag):
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
Also, note that on Android 12+, apps must be launched manually by the user at least once before they can receive BOOT_COMPLETED broadcasts.
内容的提问来源于stack exchange,提问作者HelloCW

