Android boot is simple, and android boot is simple.

Source: Internet
Author: User

Android boot is simple, and android boot is simple.

Today, we will mainly discuss how android can enable a service to automatically start upon startup. The Android mobile phone will trigger a Standard Broadcast Action during startup, called android. intent. action. BOOT_COMPLETED (remember to trigger only once). Here we can build a broadcast receiver to receive this action. below is a simple example of the following implementation steps:

Step 1: Create a broadcast receiver and reconstruct its abstract method onReceive (Context context, Intent intent) to start the Service or app you want to start.

Import android. content. BroadcastReceiver;

Import android. content. Context;

Import android. content. Intent;

Import android. util. Log;

Public class BootBroadcastReceiver extends BroadcastReceiver {

// Override the onReceive Method

@ Override public void onReceive (Context context, Intent intent ){

// The XXX. class at the end is the service to be started.

Intent service = new Intent (context, XXXclass );

Context. startService (service );

Log. v ("TAG", "automatic Service Startup .....");

// Start the application. The parameter is the package name of the application to be automatically started.

Intent intent2 = context. getPackageManager (). getLaunchIntentForPackage (packageName );

Context. startActivity (intent2 );

}

}

Step 2: configure the xml file and add the intent-filter configuration in the receiver.

<Cycler android: name = "BootBroadcastReceiver">

<Intent-filter>

<Action android: name = "android. intent. action. BOOT_COMPLETED"> </action>

<Category android: name = "android. intent. category. LAUNCHER"/>

</Intent-filter>

</Cycler>

Step 3: add permissions <uses-permission android: name = "android. permission. RECEIVE_BOOT_COMPLETED"/>

In this way, you can start the system on your own, but there is a problem that sometimes it may take several seconds to enable a certain latency. It may be that the system did not respond to the problem and the broadcast was not sent in time...

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.