Android interface boot Activity

1 , Principle :

    

    When the Android system to complete BOOT later stage, will send a named  ACTION_BOOT_COMPLETED  broadcast, we will come out in a BroadcastReceiver capture this broadcast, and then start our Activity or Service , of course, to note that our application must capture the broadcast rights, see the following specific steps:

    

The first step: First, there must be one for the start-up of Activity or Service , here in the simplest system to create their own Activity example to explain.

   

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 want to write a BroadcastReceiver to capture ACTION_BOOT_COMPLETED this broadcast, and we start to be started after the capture Activity .

   

   

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 you must add the mark, or fails to start 

      context.startActivity(newIntent);   

// Note: If you are going to see the boot program uses startActivity, if you wish to start a service with startService    

    }       

  } 

}

   

The third step: in AndroidManifest.xml Sign up for our profile BroadcastReceiver

   

<?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 a point: 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 Point two: plus receivers content  -> 

        <receiver android:name=".BootCompletedReceiver"> 

            <intent-filter> 

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

            </intent-filter> 

       </receiver> 

    </application> 

</manifest> 

   

来自 <http://tool.oschina.net/highlight>

Reproduced in: https: //www.cnblogs.com/postmaster/p/3756050.html

Guess you like

Origin blog.csdn.net/weixin_34375251/article/details/93507533