Android Implementation program starts from boot

Source: Internet
Author: User

In Android, to achieve the app boot automatically, you need to implement the interception broadcast Android.permission.RECEIVE_BOOT_COMPLETED, And you need to use the method of statically registering the broadcast (that is, the broadcast is defined in the Androidmanifest.xml file)

1. Define broadcast and claim permissions in the Androidmanifest.xml file first

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

<receiver
Android:name= ". Myreceiver "
Android:enabled= "true"
Android:exported= "true" >
<intent-filter android:priority= ">"
<action android:name= "Android.intent.action.BOOT_COMPLETED" ></action>
</intent-filter>
</receiver>

The above myreceiver is my own writing class, inherited the Broadcastreceiver, the code is as follows:

public class Myreceiver extends broadcastreceiver
{
Public Myreceiver ()
{
}

@Override
public void OnReceive (context context, Intent Intent)
{
if (Intent.getaction (). Equals ("Android.intent.action.BOOT_COMPLETED"))
{
Intent i = new Intent (context, mainactivity.class);
I.addflags (Intent.flag_activity_new_task);
Context.startactivity (i);
}
}
}
Above the i.addflags (intent.flag_activity_new_task); Very important, if missing, the program will be in the start times wrong

2, the following is the code of the Mainactivity class

public class Mainactivity extends actionbaractivity
{
@Override
protected void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Toast.maketext (This, "Haha, I have successfully started!" ", Toast.length_long). Show ();
LOG.E ("AutoRun", "Haha, I have successfully started!" ");
}
}

3, install the app to the phone, and then start a program (it is said that after Android 4.0, you must first start a program to receive a boot-completed broadcast, to prevent malicious programs)

4. Restart your phone to test if the app is automatically started. If there is, then congratulate you. If not, please look down.

What if the restart does not start the program after you follow all the above steps? What's going on here? So first please check if your phone is installed 360 such as security software, if there is, please set the app to "allow" in the software's self-launch software management (my phone does not have these software installed, but the system settings have self-initiated software management features, so I am here to set my app to " Allow boot to start "), restart your phone and test for success. If it still fails, then please check your phone is not set the app installation preferred location is the SD card, it is said to install to the SD card, because the phone started successfully (after the launch of the completion of the broadcast) before loading the SD card, so the app does not receive the broadcast. If so, install the app to internal storage and try it out. If you do not know the settings, then directly set the installation path in the Androidmanifest.xml file, android:installlocation= "Internalonly". Like what:

<manifest
Package= "Cn.weixq.autorun"
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:installlocation= "Internalonly">

Android Implementation program starts from boot

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.