Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 高級開發 >> Android 開發 — 開機自啟動

Android 開發 — 開機自啟動

編輯:高級開發

 android 的開機自啟動是通過捕捉開機結束的廣播來實現的,手機的啟動完後會給出一個BroadcastReceiver,在自己的程序中捕捉即可。

  AutoBootReceiver.Java 文件

  Java代碼

  1. package com.ldq.auto.boot;

  2.

  3. import android.content.BroadcastReceiver;

  4. import android.content.Context;

  5. import android.content.Intent;

  6. import android.util.Log;

  7.

  8. public class AutoBootReceiver extends BroadcastReceiver {

  9.

  10. @Override

  11. public void onReceive(Context context, Intent intent) {

  12. // TODO Auto-generated method stub

  13. if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {

  14. Log.i("------", "AutoBootReceiver auto boot");

  15. Intent in = new Intent(context, ExAutoBoot.class);

  16. in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//添加Flags必不可少,否則報錯

  17. context.startActivity(in);

  18. }

  19. }

  20. }

  package com.ldq.auto.boot;

  import android.content.BroadcastReceiver;

  import android.content.Context;

  import android.content.Intent;

  import android.util.Log;

  public class AutoBootReceiver extends BroadcastReceiver {

  @Override

  public void onReceive(Context context, Intent intent) {

  // TODO Auto-generated method stub

  if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {

  Log.i("------", "AutoBootReceiver auto boot");

  Intent in = new Intent(context, ExAutoBoot.class);

  in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//添加Flags必不可少,否則報錯

  context.startActivity(in);

  }

  }

  }

  ExAutoBoot.Java 文件:

  Java代碼

  1. package com.ldq.auto.boot;

  2.

  3. import android.app.Activity;

  接上頁

  4. import android.os.Bundle;

  5. import android.util.Log;

  6.

  7. public class ExAutoBoot extends Activity {

  8. /** Called when the activity is first created. */

  9. @Override

  10. public void onCreate(Bundle savedInstanceState) {

  11. super.onCreate(savedInstanceState);

  12. setContentVIEw(R.layout.main);

  13. Log.i("------","ExAutoBoot auto boot");

  14. }

  15. }

  package com.ldq.auto.boot;

  import android.app.Activity;

  import android.os.Bundle;

  import android.util.Log;

  public class ExAutoBoot extends Activity {

  /** Called when the activity is first created. */

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentVIEw(R.layout.main);

  Log.i("------","ExAutoBoot auto boot");

  }

  }

  androidManifest.XML 文件:

  Java代碼

  1. < ?XML version="1.0" encoding="utf-8"?>

  2. < manifest XMLns:android="http://schemas.android.com/apk/res/android"

  3. package="com.ldq.auto.boot" android:versionCode="1"

  4. android:versionName="1.0">

  5. < application android:icon="@drawable/icon" android:label="@string/app_name">

  6. < activity android:name=".ExAutoBoot" android:label="@string/app_name">

  7. < intent-filter>

  8. < action android:name="android.intent.action.MAIN" />

  9. < category android:name="android.intent.category.LAUNCHER" />

  10. < /intent-filter>

  11. < /activity>

  12. < receiver android:name="AutoBootReceiver">

  13. < intent-filter>

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

  接上頁

/action>

  15. < /intent-filter>

  16. < /receiver>

  17. < /application>

  18. < uses-sdk android:minSdkVersion="4" />

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

  20. < /manifest>

  < ?XML version="1.0" encoding="utf-8"?>

  < manifest XMLns:android="http://schemas.android.com/apk/res/android"

  package="com.ldq.auto.boot" android:versionCode="1"

  android:versionName="1.0">

  < application android:icon="@drawable/icon" android:label="@string/app_name">

  < activity android:name=".ExAutoBoot" android:label="@string/app_name">

  < intent-filter>

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

  < category android:name="android.intent.category.LAUNCHER" />

  < /intent-filter>

  < /activity>

  < receiver android:name="AutoBootReceiver">

  < intent-filter>

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

  < /intent-filter>

  < /receiver>

  < /application>

  < uses-sdk android:minSdkVersion="4" />

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

  < /manifest>

  教你開啟自啟動程序! 在電腦的操作系統中都能在電腦開機後自啟動一些程序,在android平台也是可以的,那麼我們如何才能做到這樣的效果呢。

  1、需要BroadcastReceiver

  2、使用interfilter中的action:android.intent.action.BOOT_COMPLETED //當啟動手機系統啟動完成後就啟動此Receiver

  功能:當手機系統啟動完成後,直接啟動某個程序或者Activity,這裡直接啟動某個程序

  一下是代碼分析:

  androidManifest.XML文件內容:

  接上頁

  XML代碼

  1. # < ?XML version="1.0" encoding="utf-8"?>

  2. # < manifest XMLns:android="http://schemas.android.com/apk/res/android"

  3. # package="cc.androidos.sms"

  4. # android:versionCode="1"

  5. # android:versionName="1.0.0">

  6. # < application android:icon="@drawable/icon" android:label="@string/app_name">

  7. # < activity android:name=".StartUp"

  8. # android:label="@string/app_name">

  9. # < intent-filter>

  10. # < action android:name="android.intent.action.MAIN" />

  11. # < category android:name="android.intent.category.LAUNCHER" />

  12. # < /intent-filter>

  13. # < /activity>

  14. #

  15. # < receiver android:name=".BootReceiver">

  16. # < intent-filter>

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

  18. # < /intent-filter>

  19. # < /receiver>

  20. # < service android:name=".StartService"/>

  21. # < /application>

  22. # < /manifest>

  # < ?XML version="1.0" encoding="utf-8"?>

  # < manifest XMLns:android="http://schemas.android.com/apk/res/android"

  # package="cc.androidos.sms"

  # android:versionCode="1"

  # android:versionName="1.0.0">

  # < application android:icon="@drawable/icon" android:label="@string/app_name">

  # < activity android:name=".StartUp"

  # android:label="@string/app_name">

  # < intent-filter>

  # < action android:name="android.intent.action.MAIN" />

  # < category android:name="android.intent.category.LAUNCHER" />

  # < /intent-filter>

  # < /activity>

  #

  # < receiver android:name=".BootReceiver">

  接上頁

  # < intent-filter>

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

  # < /intent-filter>

  # < /receiver>

  # < service android:name=".StartService"/>

  # < /application>

  # < /manifest>

  要啟動的Activity類:

  Java代碼

  1. # package cc.androidos.sms;

  2. # import android.app.Activity;

  3. # import android.os.Bundle;

  4. # public class StartUp extends Activity {

  5. # /** Called when the activity is first created. */

  6. # @Override

  7. # public void onCreate(Bundle savedInstanceState) {

  8. # super.onCreate(savedInstanceState);

  9. # setContentVIEw(R.layout.main);

  10. # }

  11. # }

  # package cc.androidos.sms;

  # import android.app.Activity;

  # import android.os.Bundle;

  # public class StartUp extends Activity {

  # /** Called when the activity is first created. */

  # @Override

  # public void onCreate(Bundle savedInstanceState) {

  # super.onCreate(savedInstanceState);

  # setContentVIEw(R.layout.main);

  # }

  # }

  Receiver類:系統啟動後接受信息的類

  Java代碼

  1. # package cc.androidos.sms;

  2. # import android.app.Activity;

  3. # import android.app.PendingIntent;

  4. # import android.content.BroadcastReceiver;

  5. # import android.content.Context;

  6. # import android.content.Intent;

  7. # import android.Net.Uri;

  8. # import android.util.Log;

  9. # public class BootReceiver extends BroadcastReceiver

  10. # {

  11. # @Override

  12. # public void onReceive( Context context, Intent intent )

  13. # {

  14. # if(intent.getAction().equals( Intent.ACTION_BOOT_COMPLETED )){

  接上頁

  15. # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  16. # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  17. # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  18. # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  19. # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  20. # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  21. # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  22. # Intent i = new Intent(context,StartUp.class);

  23. # i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );

  24. # //使用Receiver直接啟動Activity時候需要加入此flag,否則系統會出現異常

  25. # context.startActivity( i );

  26. #

  27. # }

  28. # }

  29. # }

  # package cc.androidos.sms;

  # import android.app.Activity;

  # import android.app.PendingIntent;

  # import android.content.BroadcastReceiver;

  # import android.content.Context;

  # import android.content.Intent;

  # import android.Net.Uri;

  # import android.util.Log;

  # public class BootReceiver extends BroadcastReceiver

  接上頁

  # {

  # @Override

  # public void onReceive( Context context, Intent intent )

  # {

  # if(intent.getAction().equals( Intent.ACTION_BOOT_COMPLETED )){

  # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  # Log.d( ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>", "boot start................" );

  # Intent i = new Intent(context,StartUp.class);

  # i.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK );

  # //使用Receiver直接啟動Activity時候需要加入此flag,否則系統會出現異常

  # context.startActivity( i );

  #

  # }

  # }

  # }

  第一個運行完成後,關閉手機模擬器或者手機,然後啟動手機操作系統,啟動完成後StartUp Activity會自動運行。

  流程: 系統啟動完成-------》通過androidManifest.XML了解到系統啟動完成後要啟動BootReceiver -------》BootReceiver 啟動StartUp Activity。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved