Android Boot Activity interface

Source: Internet
Author: User

1 , principle :

when the Android system finishes the BOOT phase, a broadcast called action_boot_completed is sent and we can Broadcastreceiver capture This broadcast, then start our Activity or Service, and of course note that our application You must have permission to capture the broadcast, see the following steps:

The first step is to have an activity or Servicefor Start-up, which is explained by the simplest activity created by the system itself.

Package com.billhoo.study;

Import android.app.Activity;

Import android.os.Bundle;

Public class boottestactivity extends Activity {

/** called when the activity is first created. * /  

@Override  

Public void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (R.layout.main);

}

}

Brother Two steps: We are going to write a broadcastreceiver to capture action_boot_completed This broadcast and start the Activitywe want to start after capturing.

Package com.billhoo.study;

Import android.content.BroadcastReceiver;

Import android.content.Context;

Import android.content.Intent;

Public class bootcompletedreceiver extends broadcastreceiver {

@Override  

   public   void   onreceive (context context, intent intent)  { 

if (Intent.getaction (). Equals (intent.action_boot_completed))

{

Intent newintent = new Intent (context, boottestactivity. Class);

newintent.addflags (Intent.flag_activity_new_task); Note that this tag must be added or the start will fail

Context.startactivity (newintent);

// Note: If it is the boot will see the program to use startactivity, If you want to boot a service to use StartService

}

}

}

Step Three: Register our broadcastreceiver in the androidmanifest.xml configuration file

<?xml version="1.0" encoding= " utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  

Package ="Com.billhoo.study" android:versioncode= " 1"

Android:versionname= "1.0" >

<uses-sdk android:minsdkversion= "4" />

<!-- Note One: You must add permission permissions --

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

<application android:icon= "@drawable/icon" android:label= "@string/app_name" >

<!--Activities--

<activity android:name= ". Boottestactivity " android:label=" @string/app_name ">

<intent-filter>

<action android:name= "Android.intent.action.MAIN" />

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

</intent-filter>

</activity>

<!-- Note Two: Add receivers Content- -

<receiver android:name= ". Bootcompletedreceiver ">

<intent-filter>

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

</intent-filter>

</receiver>

</application>

</manifest>

From

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.