- package ss.pku.edu.cn;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- public class LaunchReceiver extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Intent intent1 = new Intent(context , MyService.class);
- // 啓動指定Server
- context.startService(intent1);
- }
- }
- package ss.pku.edu.cn;
- import android.app.Service;
- import android.content.Intent;
- import android.os.IBinder;
- public class MyService extends Service
- {
- @Override
- public IBinder onBind(Intent intent)
- {
- return null;
- }
- @Override
- public void onCreate()
- {
- System.out.println("service create");
- }
- }
然後在AndroidManifest.xml 進行添加 權限和action
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <receiver android:name=".LaunchReceiver">
- <intent-filter>
- <action android:name="android.intent.action.BOOT_COMPLETED"/>
- </intent-filter>
- </receiver>
- <service android:name=".MyService">
- t;/service>
- </application>
本文轉自:http://blog.csdn.net/pku_android/article/details/7329080