[Android] listens for boot by listening to boot_completed and media_mounted

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.