Java boot up

Source: Internet
Author: User

1. Authorization:

<uses-permission android:name= "Android.permission.RECEIVE_BOOT_COMPLETED" ></uses-permission>

2. Intent-filter & name use class full name

<receiver
Android:name= "Com.bd.testbootservice.MyReceiver"
Android:enabled= "true"
Android:exported= "true" >

<intent-filter android:priority= ">"
<action android:name= "Android.intent.action.BOOT_COMPLETED" ></action>
<category android:name= "Android.intent.category.HOME" ></category>
</intent-filter>
</receiver>

3. Avoid using SD card:

<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.bd.testbootservice"
android:installlocation= "Internalonly" >

Android API Level8 above, the program can be installed on the SD card. If the program is installed on an SD card, the SD card will not mount after the boot_completed broadcast is sent, so the program cannot listen to the broadcast.

WORKAROUND: Simultaneously monitor the boot and SD card mount. (You can't just monitor the mount and think it's on, because some phones don't have an SD card)

The implementation of the mount for monitoring MEDA mounted as follows:

<receiver android:name= "Com.bd.testbootservice.MyReceiver" >
<intent-filter >
<action android:name= "Android.intent.action.MEDIA_MOUNTED"/>
<action android:name= "Android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme= "File" >
</data>
</intent-filter>
</receiver>
Listen to MEDA mounted receiver will not write.

How to implement a broadcast receiver that can monitor the boot boot_completed and mount Meda mounted simultaneously?

Theoretically as long as the MEDA mounted intent-filter and boot_completed Intent-filter put together on the line, but, put in the same intent-filter, boot complete can not monitor, Need to be placed in two intent filter:


<receiver android:name= "Com.bd.testbootservice.MyReceiver" >
<intent-filter >
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
<intent-filter >
<action android:name= "Android.intent.action.MEDIA_MOUNTED"/>
<action android:name= "Android.intent.action.MEDIA_UNMOUNTED"/>
<data android:scheme= "File" >
</data>
</intent-filter>
</receiver>

4. Stopped State

A
In Android development, many applications for various purposes, want to wake up when the machine starts, the general practice is to write a broadcastreceiver, receive the corresponding boot action, of course, do not forget to add permission in manifest " Android.permission.RECEIVE_BOOT_COMPLETED ". But recently in the 4.0 development, some colleagues claimed that the broadcast was not received, while others said their own can receive, what is the matter.

It turns out that after 3.1, the system's Package manager adds management to the "Stopped state" application, which is completely different from the stop status in the stopped and activity lifecycle. Refers to an application that has never been started after installation and is manually forced to stop by the user, while the system adds 2 flag:flag_include_stopped_packages and Flag_exclude_stopped_packages, To identify whether a intent activates an app that is in "stopped state". When 2 flags are not set or are set, the Flag_include_stopped_packages effect is used.

With the new mechanism above, Google think to all the broadcast intent default plus flag_exclude_stopped_packages will be very cooooool, to a certain extent, to avoid the rogue software, virus ah bad, but also to improve efficiency, Causes the problem in the topic of this article, receive_boot_completed broadcast if the user has not run the application, it will not respond.

But Google still leaves room to allow apps and backend services to wake up the STOPPED state program by setting Flag_include_stopped_packages to broadcast intent. That is, users write their own broadcast intent can control this mechanism, but the system comes with the broadcast intent, because can not be modified, can only accept the reality.

Two

Android 4.0 to prevent some malware (malicious software) from running in the background without user startup, the default installed program is completely ignored by the system before the user starts, even if the program registers the broadcast, the system will not pass the broadcast to the program. The message broadcast will not take effect until the program has been run by the user. Moreover, even if the program is already running, if the user forces stop (force stopped), it will return to the state ignored by the system. This problem has been in fact since 3.1, the recent use of 4.0 of the system testing only to find the existence of this problem, causing a lot of trouble.

5. Send command via adb ~/library/android/sdk/platform-tools

adb send boot_completed
We can pass

ADB shell am broadcast-a Android.intent.action.BOOT_COMPLETED
command to send boot_completed broadcasts without restarting the test machine or simulator to test the boot_completed broadcast, this command can be sent more precisely to a package, as follows:

ADB shell am broadcast-a android.intent.action.boot_completed-c android.intent.category.home-n Package_name/class_ Name

6.

7.

Reference:

http://www.trinea.cn/android/android-boot_completed-not-work/

Java boot up

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.