Service start-up and self-booting (there may be a problem due to Android permissions control)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.