Android开发之如何让程序开机启动

jopen 10年前

做一个开机启动的应用程序,这样就可以很轻松的做一个没有界面的开机启动程序了,实现步骤比较简单。

新建一个Android工程,名字随机,在MainActivity所在包下面新建一个

BootBroadcastReceiver.java:

public class BootReceiverextends BroadcastReceiver {    staticfinal String action_boot ="android.intent.action.BOOT_COMPLETED";    @Override    public void onReceive(Context context, Intent intent) {    // TODO Auto-generated method stub    Intent bootStartIntent = new Intent(context, MainActivity.class);    bootStartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    context.startActivity(bootStartIntent);    }    }

AndroidMainfest.xml:

<?xmlversion="1.0"encoding="utf-8"?>    <manifestxmlns:android="http://schemas.android.com/apk/res/android"        package="com.example.bootinit"        android:versionCode="1"        android:versionName="1.0">        <uses-sdk            android:minSdkVersion="8"            android:targetSdkVersion="18"/>        <uses-permissionandroid:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- 开机自启动所需用的权限 -->                <application            android:allowBackup="true"            android:icon="@drawable/ic_launcher"            android:label="@string/app_name"            android:theme="@style/AppTheme">            <activity                android:name="com.example.bootinit.MainActivity"                android:label="@string/app_name">                <intent-filter>                    <actionandroid:name="android.intent.action.MAIN"/>                    <categoryandroid:name="android.intent.category.LAUNCHER"/>                </intent-filter>            </activity>            <receiverandroid:name=".BootReceiver">                <intent-filter>                    <actionandroid:name="android.intent.action.BOOT_COMPLETED"/>                    <categoryandroid:name="android.intent.category.HOME"/>                </intent-filter>            </receiver>        </application>    </manifest>