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