Bootreceiver.java
PackageCOM.HANQI.TESTBR;ImportAndroid.content.BroadcastReceiver;ImportAndroid.content.Context;Importandroid.content.Intent;ImportAndroid.util.Log; Public classBootreceiverextendsBroadcastreceiver { PublicBootreceiver () {LOG.E ("ATG", "Construction boot complete broadcast receiver"); } @Override Public voidOnReceive (Context context, Intent Intent) {LOG.E ("ATG", "broadcast receiver automatically start service"); //Start the serviceContext.startservice (NewIntent (Context,bootservice.class)); }}
Bootservice.java
PackageCOM.HANQI.TESTBR;ImportAndroid.app.Service;Importandroid.content.Intent;ImportAndroid.os.IBinder;Importandroid.support.annotation.Nullable;ImportAndroid.util.Log;/*** Created by Administrator on 2016/4/25.*/ Public classBootserviceextendsService {@Nullable @Override Publicibinder onbind (Intent Intent) {return NULL; } @Override Public voidonCreate () {Super. OnCreate (); LOG.E ("ATG", "Service auto-start"); }}
Start the broadcast on boot