[Android執行個體] android中開機啟動案例

來源:互聯網
上載者:User

  這是個很簡單的過程,這個不像pc機得開機啟動,要有精確的記憶體位址,這裡所謂的簡單是跟pc機得開機啟動相比。android系統的宗旨說的是,不要來找我,我會來找你。

       Activity好像是應用程式的眼睛,用眼神與使用者交流而使用者則用手指頭告訴Activity我想要什麼。BroadcastReceiver好比android程式的耳朵,接受來自各方的Intent。Service好比android應用程式的手,正確完成耳朵接收到得訊息,最後,我 來 組 成 頭 部。雖然本人只對Activity是眼睛這句話有深刻的理解,剩下兩句沒有第一句感覺那麼精闢,但是直覺三句話都很精闢所以從書上抄下來了。

  
       過程是這樣的,當所有的android系統服務啟動完成以後,會像發傳單一樣像外面散布訊息,這個過程就是廣播,我們需要做的就是去捕捉這個系統啟動完成的訊息,捕捉到這個訊息以後,該啟動Activity就啟動Activity,該啟動服務就啟動服務,最好的辦法就是實踐。

  
       有三個地方值得注意的,配置AndroidManifest.xml,第一個,繼承自Broadcast的自訂類需在xml檔案中註冊,第二個,繼承自Service的自訂類需在xml檔案中註冊,第三個,捕捉一個系統啟動的廣播訊息。貼代碼 開始

        這個是接收廣播的類,

  1. package opq.broadcast;   
  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 BootBroadCastReceiver extends BroadcastReceiver{   
  9.     public static final String ACTION = "android.intent.action.BOOT_COMPLETED";   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {   
  12.         // TODO Auto-generated method stub   
  13.         if (intent.getAction().equals(ACTION)) {      
  14.             Log.d("TAG","ok");      
  15.             Intent myIntent=new Intent();//intent對象 用於啟動服務   
  16.             myIntent.setAction("opq.service.BootService");   
  17.             context.startService(myIntent);//開機 啟動服務   
  18.         }   
  19.     }   
  20. }  

複製代碼

這個是接收到廣播以後啟動的服務類,繼承自Service 必須實現onBind(Intent intent)這個方法,onCreate,只會第一次啟動服務的時候調用一次,以後除非重新啟動服務才會調用,onStart方法不管是不是第一次啟動服務都會調用的方法。

  1. package opq.service;   
  2.   
  3. import opq.boot.R;   
  4. import android.app.Service;   
  5. import android.content.Intent;   
  6. import android.media.MediaPlayer;   
  7. import android.os.IBinder;   
  8. import android.provider.MediaStore.Audio.Media;   
  9. import android.util.Log;   
  10.   
  11. public class BootService extends Service{   
  12.     MediaPlayer mediaPlayer;   
  13.     @Override  
  14.     public IBinder onBind(Intent intent) {   
  15.         // TODO Auto-generated method stub   
  16.         return null;   
  17.     }   
  18.     @Override  
  19.     public void onCreate() {   
  20.         // TODO Auto-generated method stub   
  21.         Log.d("TAG","BootService onCreate");   
  22.         mediaPlayer=MediaPlayer.create(getApplicationContext(), R.raw.anhao);   
  23.            
  24.         super.onCreate();   
  25.     }   
  26.     @Override  
  27.     public void onStart(Intent intent, int startId) {   
  28.         // TODO Auto-generated method stub   
  29.         Log.d("TAG","BootService onStart");   
  30.         mediaPlayer.start();   
  31.         mediaPlayer.setLooping(false);   
  32.         super.onStart(intent, startId);   
  33.     }   
  34. }  

複製代碼

整個程式如果你願意,只有兩個類,Activity都是多餘的。這個裡面的R.raw.anhao;raw是建立的檔案夾,在res目錄下面。

        最後吧xml檔案貼出來:在xml裡面本人就把類名配錯過,找了半天才看出來的,這玩意測試的過程比較麻煩,每次都要重啟模擬器

java代碼:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="opq.boot"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="7" />
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".BootmusicActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. <receiver android:name="opq.broadcast.BootBroadCastReceiver">
  16. <intent-filter>
  17. <action android:name="android.intent.action.BOOT_COMPLETED" />
  18. </intent-filter>
  19. </receiver>
  20. <service android:name="opq.service.BootService">
  21. <intent-filter>
  22. <action android:name="opq.service.BootService"/>
  23. </intent-filter>
  24. </service>
  25. </application>
  26. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  27. </manifest>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.