Android 開機自動啟動服務

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   get   

在前面的文章中提到了remote service 的建立過程,現在我們要讓它開機自動啟動

 

1.在前面代碼的基礎上添加 RemoteServiceBootReceiver.java ,實現一個intent的receiver

[java] view plaincopyprint?
  1. package com.fly;  
  2. import android.content.BroadcastReceiver;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. import android.util.Log;  
  6. public class RemoteServiceBootReceiver extends BroadcastReceiver {  
  7.     private static final String TAG = "U0fly RemoteServiceBootReceiver";  
  8.     static final String ACTION = "android.intent.action.BOOT_COMPLETED";  
  9.     @Override  
  10.     public void onReceive(Context arg0, Intent arg1) {  
  11.         Log.d(TAG, "Boot completed");  
  12.         // TODO Auto-generated method stub  
  13.         if (arg1.getAction().equals(ACTION)) {  
  14.             // service  
  15.             Intent myintent = new Intent(arg0, RemoteService.class);  
  16.             myintent.setAction("com.fly.RemoteService");  
  17.             arg0.startService(myintent);  
  18.         }  
  19.     }  
  20. }  

package com.fly;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public class RemoteServiceBootReceiver extends BroadcastReceiver {private static final String TAG = "U0fly RemoteServiceBootReceiver";static final String ACTION = "android.intent.action.BOOT_COMPLETED";@Overridepublic void onReceive(Context arg0, Intent arg1) {Log.d(TAG, "Boot completed");// TODO Auto-generated method stubif (arg1.getAction().equals(ACTION)) {// serviceIntent myintent = new Intent(arg0, RemoteService.class);myintent.setAction("com.fly.RemoteService");arg0.startService(myintent);}}} 

 

2.在AndroidManifast.xml中添加許可權,並註冊一個receiver

[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.fly" android:versionCode="1" android:versionName="1.0">  
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  5.         <activity android:name=".RemoteServiceActivity"  
  6.             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.         <service android:name="RemoteService">  
  13.             <intent-fliter>  
  14.                 <action android:name="com.fly.RemoteService" />  
  15.             </intent-fliter>  
  16.         </service>  
  17.       
  18.         <receiver android:name=".RemoteServiceBootReceiver">  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.BOOT_COMPLETED" />  
  21.             </intent-filter>  
  22.         </receiver>  
  23.           
  24.     </application>  
  25.       
  26.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>   
  27.     <uses-sdk android:minSdkVersion="7" />  
  28. </manifest>   

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.fly" android:versionCode="1" android:versionName="1.0"><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".RemoteServiceActivity"android:label="@string/app_name"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><service android:name="RemoteService"><intent-fliter><action android:name="com.fly.RemoteService" /></intent-fliter></service><receiver android:name=".RemoteServiceBootReceiver"><intent-filter><action android:name="android.intent.action.BOOT_COMPLETED" /></intent-filter></receiver></application><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission> <uses-sdk android:minSdkVersion="7" /></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.