歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android開機自啟動

Android開機自啟動

日期:2017/3/1 11:15:04   编辑:Linux編程

要想在Android系統中實現開機啟動,很簡單,只需要幾個步驟就可以了。

1.定義廣播類

2.Manifest.xml中注冊廣播類

3.添加權限

下面就是具體操作了。

首先,我們來定義廣播類。

創建一個類BootReceiver,使其繼承BroadcastReceiver。

重寫一些必要的Java函數

  1. package cn.etzmico;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.util.Log;
  6. public class BootReceiver extends BroadcastReceiver {
  7. public void onReceive(Context context, Intent intent) {
  8. if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
  9. Log.d("BootReceiver", "system boot completed");
  10. // context, AutoRun.class
  11. Intent newnewIntent = new Intent(context, AutoRun.class);
  12. /* MyActivity action defined in AndroidManifest.xml */
  13. newIntent.setAction("android.intent.action.MAIN");
  14. /* MyActivity category defined in AndroidManifest.xml */
  15. newIntent.addCategory("android.intent.category.LAUNCHER");
  16. /*
  17. * If activity is not launched in Activity environment, this flag is
  18. * mandatory to set
  19. */
  20. newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  21. /* if you want to start a service, follow below method */
  22. context.startActivity(newIntent);
  23. }
  24. }
  25. }

AutoRun.class就是程序運行的Activity。

其次,在Manifest.xml中注冊廣播類

  1. <receiver android:name=".BootReceiver" android:label="@string/app_name">
  2. <intent-filter>
  3. <action android:name="android.intent.action.BOOT_COMPLETED" />
  4. <category android:name="android.intent.category.LAUNCHER" />
  5. </intent-filter>
  6. </receiver>

最後,再添加上權限就可以了

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

這樣,我們就實現了Android系統的開機自啟動,切勿忘記Manifest.xml中的操作!

本文Android開機自啟動 工程資源下載:

免費下載地址在 http://linux.linuxidc.com/

用戶名與密碼都是www.linuxidc.com

具體下載目錄在 /pub/Android源碼集錦/2011年/10月/Android開機自啟動/

Copyright © Linux教程網 All Rights Reserved