You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Android模拟器中模拟重启,完成BOOT_COMPLETED与持久化任务测试?

Answers to Your Android Testing Questions

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:

  1. 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
    
  2. Reboot the Emulator: Use adb reboot as described earlier.
  3. Check Job Status via ADB: After reboot, run this command to see if your job is still registered:
    adb shell dumpsys jobscheduler | grep "info.dodata.mirror"
    
    Look for your job ID (from R.integer.JobID) in the output—if it’s present, the job persisted correctly.
  4. Add Logs for Execution Confirmation: In your RestoreService, add a log in the onStartJob method:
    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
    }
    
    Check Logcat for this message after reboot to confirm the job executed.

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:38:44