2012-02-25 359 views

回答

5

首先,你需要的許可,您的清單:

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

此外,在您的清單,定義服務和監聽引導完成的動作:

<service android:name=".MyService" android:label="My Service"> 
    <intent-filter> 
     <action android:name="com.myapp.MyService" /> 
    </intent-filter> 
</service> 

<receiver 
    android:name=".receiver.StartMyServiceAtBootReceiver" 
    android:enabled="true" 
    android:exported="true" 
    android:label="StartMyServiceAtBootReceiver"> 
    <intent-filter> 
     <action android:name="android.intent.action.BOOT_COMPLETED" /> 
    </intent-filter> 
</receiver> 

然後,你需要定義將獲得BOOT_COMPLETED操作並啓動服務的接收器。

public class StartMyServiceAtBootReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) { 
      Intent serviceIntent = new Intent("com.myapp.MySystemService"); 
      context.startService(serviceIntent); 
     } 
    } 
} 

現在你的服務應該在手機啓動時運行。

0

下面是完整的工作示例代碼,

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.practice" android:versionCode="1" android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="4" /> 

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

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".AutoStartExampleActivity" 
      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="AutoStart"></receiver> 

     <receiver android:name=".AutoStart"> 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
     </receiver> 


    </application> 
</manifest> 

AutoStartExampleActivity文件

package com.practice; 

import android.app.Activity; 
import android.os.Bundle; 

public class AutoStartExampleActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
    } 
} 

廣播接收器代碼,

package com.practice; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 

public class AutoStart extends BroadcastReceiver 
{ 

    public void onReceive(Context context, Intent intent) 
    { 
     System.out.println ("Application Started"); 
     // put your TimerTask calling class here 

     try 
     { 
      Intent myIntent = new Intent (context, AutoStartExampleActivity.class); 
      myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      context.startActivity(myIntent); 
     } 
     catch (Exception e) 
     { 
      System.out.println (" Error while Starting Activity " + e.toString()); 
     } 
    } 
} 
相關問題