1. Description
After the Android phone is turned on, the broadcast android. intent. action. BOOT_COMPLETED will be sent, and the broadcast will be listened on.
2. Code
<Uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"/>
<Cycler android: name = "com. example. restarttest. BootupReceiver">
<Intent-filter>
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
</Intent-filter>
</Cycler>
public class BootupReceiver extends BroadcastReceiver { private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED"; @Override public void onReceive(Context context, Intent intent) { if (ACTION_BOOT.equals(intent.getAction())) Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show(); }}
3. Problem
When the Android API Level8 or above is used, the program can be installed on the SD card. If the program is installed on the SD card, the SD card will be mounted after the BOOT_COMPLETED broadcast is sent, so the program cannot listen to the broadcast.
4. Solution
Listen to the mounted SD card.
5. Intent-Filter problem for simultaneous listening
If BOOT_COMPLETED and MEDIA_MOUNTED and MEDIA_EJECT are written in the same intent-filter, BOOT_COMPLETED cannot be detected. for mobile phones without an SD card, BOOT_COMPLETED can only be detected, which may cause boot failure.
<Cycler android: name = ". com. example. restarttest. BootupReceiver">
<Intent-filter android: priority = "1000">
<Action android: name = "android. intent. action. BOOT_COMPLETED"/>
</Intent-filter>
<Intent-filter android: priority = "1000">
<Action android: name = "android. intent. action. MEDIA_MOUNTED"/>
<Action android: name = "android. intent. action. MEDIA_EJECT"/>
<Data android: scheme = "file"/>
</Intent-filter>
</Cycler>
5. Reference
Android boot broadcast and shutdown Broadcast
Http://stackoverflow.com/questions/2784441/trying-to-start-a-service-on-boot-on-android
Listen to SD card mounting
Android listens to both boot completed and media mounted broadcasts.
6. Link to this article
Http://blog.csdn.net/xiaodongrush/article/details/9616113