Mainactivity as follows:
Package Cc.c;import Java.util.list;import android.os.bundle;import android.os.process;import Android.view.View; Import Android.view.view.onclicklistener;import Android.widget.button;import Android.app.activity;import Android.app.activitymanager;import Android.app.activitymanager.runningappprocessinfo;import Android.app.activitymanager.runningtaskinfo;import Android.content.componentname;import Android.content.Context /** * Demo Description: * To prevent the service from being recycled by the system. * In some cases, the service will be killed by the system. On the Internet to find a lot of methods, although all claims * can avoid being system recovery, but seemingly not reliable. * In a different way to achieve, the idea: * Use Alarmmanager timing interval (such as 5s,20s) to constantly startservice (). * (1) If the service is recycled, then service OnCreate () and OnStart () * (2) will be invoked if the service is not recycled, then only service OnStart () * will be invoked to prevent the system from being recycled by this method curve implementation * * Test environment: * android:2.3.6 + me525+ * * Test method: * 1 Deploy app to device * 2 Restart device * 3 device reboot, click to enter the app. Click on the button on the interface * 4 to view Log * * * NOTE: * 1 4.0 and above please note the implementation of the boot broadcast monitoring * 2 Note permissions * <uses-permission android:name= "Android.permission.KILL_BACKGROUND_PROCESSES"/> * <uses-permission android:name= "Android.permission.Get_tasks "/> * * Remarks: * 1 when killing a process, it is sometimes necessary to determine whether the activity to which the process belongs is displayed on the screen * The method see Gettopactivitynameandprocessname () * 2 if Have the rest of the methods, please leave a message to advise. Thank you. * */public class Mainactivity extends Activity {private Button mbutton;private long lasttime = 0;private Context mcontext ; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.main); init ();} private void Init () {mcontext = This;lasttime = System.currenttimemillis (); Mbutton = (Button) Findviewbyid (R.id.button); Mbutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (view view) {if ( System.currenttimemillis ()-Lasttime) > () {lasttime = System.currenttimemillis (); cleanmemory (MContext);}});} The impersonation service is reclaimed by the system private void cleanmemory (context context) {Runningappprocessinfo runningappprocessinfo = null; Activitymanager Activitymanager = (activitymanager) context.getsystemservice (Context.activity_service); list<runningappprocessinfo> runningappprocessinfolist = ACTivitymanager.getrunningappprocesses (); if (runningappprocessinfolist! = null) {for (int i = 0; i < Runningappprocessin Folist.size (); ++i) {runningappprocessinfo = Runningappprocessinfolist.get (i); String processName = runningappprocessinfo.processname;if (Processname.startswith ("CC.C")) {SYSTEM.OUT.PRINTLN ("--- ---> Kill the Application "); int pid = Runningappprocessinfo.pid; Process.killprocess (PID);}}} Gets the stack top activity and its owning process public static string Gettopactivitynameandprocessname (context context) {string processname=null; String Topactivityname=null; Activitymanager Activitymanager = (Activitymanager) (Context.getsystemservice (android.content.Context.ACTIVITY_ SERVICE)); list<runningtaskinfo> Runningtaskinfos = activitymanager.getrunningtasks (1); if (Runningtaskinfos! = null) {componentname f=runningtaskinfos.get (0). topactivity; String Topactivityclassname=f.getclassname (); String temp[]=topactivityclassname.split ("\ \"); Top of the stack activity name Topactivityname=temp[temp.length-1]; int Index=topactivityclassname.lastindexof ("."); Stack top activity belongs to the process name processname=topactivityclassname.substring (0, index); SYSTEM.OUT.PRINTLN ("---->topactivityname=" +topactivityname+ ", processname=" +processname); } return topactivityname+ "," +processname;}}
Bootcompletedbroadcastreceiver as follows:
Package Cc.c;import Android.app.alarmmanager;import Android.app.pendingintent;import Android.content.broadcastreceiver;import Android.content.context;import Android.content.intent;import android.os.systemclock;//monitoring on Broadcast public class Bootcompletedbroadcastreceiver extends Broadcastreceiver {@ overridepublic void OnReceive (context context, Intent Intent) {//received a power on broadcast if (Intent.getaction (). Equals (Intent.action_ boot_completed)) {sendcheckservicebroadcast (context);}} Send broadcast private void Sendcheckservicebroadcast (context context) {Intent Intent = new Intent (context, Checkservicebroadcastreceiver.class); Intent.setaction ("Com.cn.checkServicebroadcasreceiver"); Pendingintent pendingintent = pendingintent.getbroadcast (context, 0, intent, 0); Long firstime = Systemclock.elapsedrealtime (); Alarmmanager Alarmmanager = (alarmmanager) context.getsystemservice (context.alarm_service);// Send broadcast startservicealarmmanager.setrepeating (alarmmanager.elapsed_realtime_wakeup, firstime,20 * 1000, per interval 20 seconds, pendingintent);}}
Checkservicebroadcastreceiver as follows:
Package Cc.c;import Android.content.broadcastreceiver;import Android.content.context;import android.content.Intent ;//Check Servicepublic class Checkservicebroadcastreceiver extends Broadcastreceiver {@Overridepublic void OnReceive ( Context context, Intent Intent) {if (Intent.getaction (). Equals ("Com.cn.checkServicebroadcasreceiver")) {Intent Serviceintent = new Intent (); Serviceintent.setaction ("Cn.com.servicesubclass"); Context.startservice (serviceintent );}}}
Servicesubclass as follows:
Package Cc.c;import Android.app.service;import Android.content.intent;import android.os.ibinder;// Execute business logic Servicepublic class Servicesubclass extends Service {@Overridepublic ibinder onbind (Intent arg0) {return null;} @Overridepublic void OnCreate () {super.oncreate (); SYSTEM.OUT.PRINTLN ("------>service onCreate ()");} @Overridepublic void OnStart (Intent Intent, int startid) {Super.onstart (Intent, Startid); SYSTEM.OUT.PRINTLN ("------>service OnStart ()");}}
Main.xml as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <button android:id= "@+id/button" android:layout_width= "Wrap_content" android: layout_height= "Wrap_content" android:layout_centerinparent= "true" android:text= "kill process"/></ Relativelayout>
Androidmanifest.xml as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" CC.C "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:min sdkversion= "android:targetsdkversion="/> <!--Kill process permissions--<uses-permission Android:na Me= "Android.permission.KILL_BACKGROUND_PROCESSES"/> <!--get permission to a task--<uses-permission Android:name= " Android.permission.GET_TASKS "/> <application android:allowbackup=" true "android:icon=" @drawable/I C_launcher "android:label=" @string/app_name "android:theme=" @style/apptheme "> <activity Android:name= "cc.c.mainactivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android. Intent.category.LAUNCHER "/> </intent-filter> </activity> <!--Register the start-up broadcast recipient---<receiver android:name= ". Bootcompletedbroadcastreceiver "> <intent-filter> <action android:name=" Android.inte Nt.action.BOOT_COMPLETED "/> </intent-filter> </receiver> <!--registered Broadcast recipients --<receiver android:name= ". Checkservicebroadcastreceiver "> <intent-filter> <action android:name=" Com.cn.checkS Ervicebroadcasreceiver "/> </intent-filter> </receiver> <!--registration Service--> ; <service android:name= ". Servicesubclass "> <intent-filter> <action android:name=" Cn.com.servicesubclass "/> </intent-filter> </service> </application></manifest>