Android Service is not killed and priority is increased

Source: Internet
Author: User

Android Service not to be killed there are two ways of thinking, one is to set up the app as a system app. Another is to enhance the vitality of service. Can be performed even when the screen backlight is off.

Because it requires root to be set to the system application. So the latter method is generally used:

1.androidmanifest.xml Permissions License:-----------------------------------------------------------------
<uses-permission android:name= "Android.permission.WAKE_LOCK"/>

<service android:name= "Com.xx.MyService" > </service>

2. Main activity:----------------------------------------------------------------------------------------

Private Serviceconnection Conn=null;
Private Powermanager.wakelock MWL;

@Override
protected void oncreate (Bundle savedinstancestate) {
super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_mian);
powermanager pm = (powermanager) Getsystemservice (Context.power_service);
MWL = Pm.newwakelock (Powermanager.partial_wake_lock, "MyTag");
Mwl.acquire ();//Keep active after screen is off


intent service=new Intent (this,myservice.class);
startservice (service);
conn = new Serviceconnection () {
@Override
public void onservicedisconnected (componentname name) {}
@Override
public void onserviceconnected (componentname name, IBinder service) {//...}
};
bindservice (service, Conn, bind_auto_create); Bind service
}

@Override
protected void OnDestroy () {
// Mwl.release ();//release
Unbindservice ( conn);//unbind
Super.ondestroy ();
}

------------------------------------------------------------------------------------------------------

Android Service Boost Priority:

public class MyService extendsservice{@Overridepublic ibinder onbind (Intent Intent) {Notification Notification = new Noti Fication (R.drawable.ic_launcher, "title", System.currenttimemillis ());        Pendingintent pintent = Pendingintent.getservice (this, 0, intent, 0);        Notification.setlatesteventinfo (This, "service",  "service is running", pintent);        Startforeground (0, notification);//Set the highest level process. Notification with ID 0 status bar will not show return new Mybinder ();} Class Mybinder extendsbinder{//...  } @Overridepublic void OnDestroy () {Stopforeground (true);//cancels the highest-level process Super.ondestroy ();}}


Android Service is not killed and priority is increased

Related Article

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.