1. Register in Mainfest.xml
<!--self-service broadcast--
<receiver
Android:name= "Com.bdkj.gmys.broadcast.BootBroadCast"
android:permission= "Android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name= "Android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<receiver android:name= "Com.bdkj.gmys.broadcast.Alarmreceiver" >
<intent-filter>
<action android:name= "Arui.alarm.action"/>
</intent-filter>
</receiver>
2. Set the Listener (set timer, check the service at irregular time)
public class Bootbroadcast extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent mintent) {
if (Intent.ACTION_BOOT_COMPLETED.equals (Mintent.getaction ())) {
Start complete
Intent Intent = new Intent (context, alarmreceiver.class);
Intent.setaction ("Arui.alarm.action");
Pendingintent Sender = pendingintent.getbroadcast (context, 0,
Intent, 0);
Long firstime = Systemclock.elapsedrealtime ();
Alarmmanager am = (alarmmanager) context
. Getsystemservice (Context.alarm_service);
10 seconds, one cycle, no stop sending broadcasts.
Am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firstime,
* +, sender);
}
}
}
public class Alarmreceiver extends Broadcastreceiver {
@Override
public void OnReceive (context context, Intent Intent) {
if (Intent.getaction (). Equals ("Arui.alarm.action")) {
if (!isservicework (context, "Com.easemob.chat.EMChatService")) {
Ring Letter Service
Intent i = new Intent ();
I.setclass (context, emchatservice.class);
Context.startservice (i);
}
if (!isservicework (context, "Com.umeng.message.UmengService")) {
Friends Alliance Service
Intent n = new Intent ();
N.setclass (context, umengservice.class);
Context.startservice (n);
}
}
}
/**
* Methods to determine if a service is running
*
* @param mcontext
* @param serviceName
* is the package name + service class name (for example: Net.loonggg.testbackstage.TestService)
* @return True to indicate that the service is not running, false
*/
public boolean isservicework (Context mcontext, String serviceName) {
Boolean iswork = false;
Activitymanager Myam = (activitymanager) mcontext
. Getsystemservice (Context.activity_service);
list<runningserviceinfo> myList = myam.getrunningservices (40);
if (mylist.size () <= 0) {
return false;
}
for (int i = 0; i < mylist.size (); i++) {
String mname = Mylist.get (i). Service.getclassname (). toString ();
if (Mname.equals (ServiceName)) {
Iswork = true;
Break
}
}
return iswork;
}
Service start-up and self-booting (there may be a problem due to Android permissions control)