Android怎麼讓一個service開機自動啟動

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   使用   sp   java   strong   

1.首先開機啟動後系統會發出一個Standard Broadcast Action,名字叫android.intent.action.BOOT_COMPLETED,這個Action只會發出一次。

2.構造一個IntentReceiver類,重構其抽象方法onReceiveIntent(Context context, Intent intent),在其中啟動你想要啟動的Service。

3.在AndroidManifest.xml中,首先加入<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>來獲得BOOT_COMPLETED的使用許可,然後註冊前面重構的IntentReceiver類,在其<intent-filter>中加入<action android:name="android.intent.action.BOOT_COMPLETED" /> ,以使其能捕捉到這個Action。

一個例子
xml:

 

Java代碼 
  1. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>   
  2. <receiver android:name=".OlympicsReceiver" android:label="@string/app_name">    
  3.     <intent-filter>    
  4.        <action android:name="android.intent.action.BOOT_COMPLETED" />    
  5.        <category android:name="android.intent.category.LAUNCHER" />    
  6.     </intent-filter>    
  7. </receiver>  
[java] view plaincopy 
  1. <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>  
  2. <receiver android:name=".OlympicsReceiver" android:label="@string/app_name">   
  3.     <intent-filter>   
  4.        <action android:name="android.intent.action.BOOT_COMPLETED" />   
  5.        <category android:name="android.intent.category.LAUNCHER" />   
  6.     </intent-filter>   
  7. </receiver>  

 java:

 

Java代碼 
  1. public class OlympicsReceiver extends IntentReceiver    
  2. {   
  3.     /*要接收的intent源*/  
  4.     static final String ACTION = "android.intent.action.BOOT_COMPLETED";   
  5.            
  6.     public void onReceiveIntent(Context context, Intent intent)    
  7.     {   
  8.         if (intent.getAction().equals(ACTION))    
  9.         {   
  10.                   context.startService(new Intent(context,    
  11.                        OlympicsService.class), null);//啟動倒計時服務   
  12.              Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show();   
  13.         }   
  14.     }   
  15. }  
[java] view plaincopy 
  1. public class OlympicsReceiver extends IntentReceiver   
  2. {  
  3.     /*要接收的intent源*/  
  4.     static final String ACTION = "android.intent.action.BOOT_COMPLETED";  
  5.           
  6.     public void onReceiveIntent(Context context, Intent intent)   
  7.     {  
  8.         if (intent.getAction().equals(ACTION))   
  9.         {  
  10.                   context.startService(new Intent(context,   
  11.                        OlympicsService.class), null);//啟動倒計時服務  
  12.              Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show();  
  13.         }  
  14.     }  
  15. }  

 注意:現在的IntentReceiver已經變為BroadcastReceiver,OnReceiveIntent為onReceive。所以java這邊的代碼為:

 

(也可以實現應用程式開機自動啟動)Java代碼 
  1. public class OlympicsReceiver extends BroadcastReceiver   
  2. {   
  3.     /*要接收的intent源*/  
  4.     static final String ACTION = "android.intent.action.BOOT_COMPLETED";   
  5.            
  6.     public void onReceive(Context context, Intent intent)    
  7.     {   
  8.         if (intent.getAction().equals(ACTION))    
  9.         {   
  10.                   context.startService(new Intent(context,    
  11.                        OlympicsService.class), null);//啟動倒計時服務   
  12.              Toast.makeText(context, "OlympicsReminder service has started!", Toast.LENGTH_LONG).show();   
  13.             //這邊可以添加開機自動啟動的應用程式代碼   
  14.         }   
  15.     }   
  16. }  

Android怎麼讓一個service開機自動啟動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.