注册receiver
<receiver android:name=".receiver.PlayerBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
获取读的权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
在新建的receiver 里面执行 ,开始service 的操作:
public class PlayerBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if("android.intent.action.BOOT_COMPLETED".equals(action)){ Log.d("151217MY", "Started..."); Intent service = new Intent(context, StartService.class); context.startService(service); } } }
新建一个startService:
public class StartService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Intent my = new Intent(this, MyActivity.class); my.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(my); return super.onStartCommand(intent, flags, startId); } }
注册startService
<service android:name=".service.StartService"/>