android APP开机自动启动

来源:互联网 发布:符号学理论 知乎 编辑:程序博客网 时间:2024/04/25 19:44

1.编写目标类并继承BroadcastReceiver

package com.example.gsensortester;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class BootCompletedReceiver extends BroadcastReceiver{@Overridepublic void onReceive(Context arg0, Intent arg1) {final Context mContext = arg0;Log.d("BootCompletedReceiver", "recevie boot completed ... ");  if(Intent.ACTION_BOOT_COMPLETED.equals(arg1.getAction()))  {                     Log.d("BootCompletedReceiver", "Hello,world... ");               }}}

上列在收到开机广播之后输出"Hello,world".

2.AndroidManifest.xml中添加权限和注册

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.gsensortester"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="19" />    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" ><receiver android:name=".BootCompletedReceiver">      <intent-filter>            <action android:name="android.intent.action.BOOT_COMPLETED" />        <category android:name="android.intent.category.DEFAULT" />        </intent-filter>  </receiver>             </application></manifest>

主要是向其中注册广播接收器:

<receiver android:name=".BootCompletedReceiver">      <intent-filter>            <action android:name="android.intent.action.BOOT_COMPLETED" />        <category android:name="android.intent.category.DEFAULT" />        </intent-filter>  </receiver> 
以及添加开机广播接收的权限:

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


0 0
原创粉丝点击