1.AndroidManifest.xml中注册一个receiver,并且获取开机启动权限permission
<receiver android:name="LaunchReceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver>
2.建立一个名为LaunchReceiver的Broadcasteceiver类,启动相应的服务。
package project.observe; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; //设置开机启动Service public class LaunchReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub Intent myIntent = new Intent(context, MyService.class); context.startService(myIntent); } }