這是個很簡單的過程,這個不像pc機得開機啟動,要有精確的記憶體位址,這裡所謂的簡單是跟pc機得開機啟動相比。android系統的宗旨說的是,不要來找我,我會來找你。
Activity好像是應用程式的眼睛,用眼神與使用者交流而使用者則用手指頭告訴Activity我想要什麼。BroadcastReceiver好比android程式的耳朵,接受來自各方的Intent。Service好比android應用程式的手,正確完成耳朵接收到得訊息,最後,我 來 組 成 頭 部。雖然本人只對Activity是眼睛這句話有深刻的理解,剩下兩句沒有第一句感覺那麼精闢,但是直覺三句話都很精闢所以從書上抄下來了。
過程是這樣的,當所有的android系統服務啟動完成以後,會像發傳單一樣像外面散布訊息,這個過程就是廣播,我們需要做的就是去捕捉這個系統啟動完成的訊息,捕捉到這個訊息以後,該啟動Activity就啟動Activity,該啟動服務就啟動服務,最好的辦法就是實踐。
有三個地方值得注意的,配置AndroidManifest.xml,第一個,繼承自Broadcast的自訂類需在xml檔案中註冊,第二個,繼承自Service的自訂類需在xml檔案中註冊,第三個,捕捉一個系統啟動的廣播訊息。貼代碼 開始
這個是接收廣播的類,
- package opq.broadcast;
-
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.util.Log;
-
- public class BootBroadCastReceiver extends BroadcastReceiver{
- public static final String ACTION = "android.intent.action.BOOT_COMPLETED";
- @Override
- public void onReceive(Context context, Intent intent) {
- // TODO Auto-generated method stub
- if (intent.getAction().equals(ACTION)) {
- Log.d("TAG","ok");
- Intent myIntent=new Intent();//intent對象 用於啟動服務
- myIntent.setAction("opq.service.BootService");
- context.startService(myIntent);//開機 啟動服務
- }
- }
- }
複製代碼
這個是接收到廣播以後啟動的服務類,繼承自Service 必須實現onBind(Intent intent)這個方法,onCreate,只會第一次啟動服務的時候調用一次,以後除非重新啟動服務才會調用,onStart方法不管是不是第一次啟動服務都會調用的方法。
- package opq.service;
-
- import opq.boot.R;
- import android.app.Service;
- import android.content.Intent;
- import android.media.MediaPlayer;
- import android.os.IBinder;
- import android.provider.MediaStore.Audio.Media;
- import android.util.Log;
-
- public class BootService extends Service{
- MediaPlayer mediaPlayer;
- @Override
- public IBinder onBind(Intent intent) {
- // TODO Auto-generated method stub
- return null;
- }
- @Override
- public void onCreate() {
- // TODO Auto-generated method stub
- Log.d("TAG","BootService onCreate");
- mediaPlayer=MediaPlayer.create(getApplicationContext(), R.raw.anhao);
-
- super.onCreate();
- }
- @Override
- public void onStart(Intent intent, int startId) {
- // TODO Auto-generated method stub
- Log.d("TAG","BootService onStart");
- mediaPlayer.start();
- mediaPlayer.setLooping(false);
- super.onStart(intent, startId);
- }
- }
複製代碼
整個程式如果你願意,只有兩個類,Activity都是多餘的。這個裡面的R.raw.anhao;raw是建立的檔案夾,在res目錄下面。
最後吧xml檔案貼出來:在xml裡面本人就把類名配錯過,找了半天才看出來的,這玩意測試的過程比較麻煩,每次都要重啟模擬器
java代碼:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="opq.boot"
- android:versionCode="1"
- android:versionName="1.0">
- <uses-sdk android:minSdkVersion="7" />
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".BootmusicActivity"
- 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="opq.broadcast.BootBroadCastReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED" />
- </intent-filter>
- </receiver>
- <service android:name="opq.service.BootService">
- <intent-filter>
- <action android:name="opq.service.BootService"/>
- </intent-filter>
- </service>
- </application>
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- </manifest>