Android開機(jī)自啟動(dòng)具體操作方法簡(jiǎn)介
在模擬器中對(duì)Android 操作系統(tǒng)進(jìn)行相應(yīng)的編寫,可以幫助我們實(shí)現(xiàn)應(yīng)用程序的開機(jī)自啟動(dòng)功能。在這里我們就來(lái)通過一段代碼充分的掌握Android開機(jī)自啟動(dòng)的相關(guān)實(shí)現(xiàn)方法,以幫助大家掌握這一應(yīng)用。
1.定義一個(gè)BroadcastReceiver
Java代碼
- public class BootReceiver extends BroadcastReceiver {
- public void onReceive(Context ctx, Intent intent) {
- Log.d("BootReceiver", "system boot completed");
- //start activity
- String action="android.intent.action.MAIN";
- String category="android.intent.category.LAUNCHER";
- Intent myi=new Intent(ctx,CustomDialog.class);
- myi.setAction(action);
- myi.addCategory(category);
- myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- ctx.startActivity(myi);
- //start service
- Intent s=new Intent(ctx,MyService.class);
- ctx.startService(s);
- }
- }
- public class BootReceiver extends BroadcastReceiver {
- public void onReceive(Context ctx, Intent intent) {
- Log.d("BootReceiver", "system boot completed");
- //start activity
- String action="android.intent.action.MAIN";
- String category="android.intent.category.LAUNCHER";
- Intent myi=new Intent(ctx,CustomDialog.class);
- myi.setAction(action);
- myi.addCategory(category);
- myi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- ctx.startActivity(myi);
- //start service
- Intent s=new Intent(ctx,MyService.class);
- ctx.startService(s);
- }
- }
2.配置Receiver的許可,允許接收系統(tǒng)啟動(dòng)消息,在AndroidManifest.xml中:
Xml代碼
- < uses-permission android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"/>- < uses-permission android:name=
"android.permission.RECEIVE_BOOT_COMPLETED"/>
3.配置Receiver,可以接收系統(tǒng)啟動(dòng)消息,在AndroidManifest.xml中
Android開機(jī)自啟動(dòng)的Xml代碼
- < receiver android:name=".app.BootReceiver">
- < intent-filter>
- < action android:name="android.intent.action.BOOT_COMPLETED"/>
- < category android:name="android.intent.category.HOME" />
- < /intent-filter>
- < /receiver>
- < receiver android:name=".app.BootReceiver">
- < intent-filter>
- < action android:name="android.intent.action.BOOT_COMPLETED"/>
- < category android:name="android.intent.category.HOME" />
- < /intent-filter>
- < /receiver>
4.啟動(dòng)模擬器,可以看到系統(tǒng)啟動(dòng)后,彈出一個(gè)對(duì)話框。
Android開機(jī)自啟動(dòng)的具體方法就為大家介紹到這里。
【編輯推薦】