Android to completely exit the app/erase data/Uninstall Application method

Source: Internet
Author: User

Clear application Data and uninstall apps, execute commands directly:

* String cmd = "pm Clear" + PackageName; or * String cmd = "pm Clear" + PackageName + "here";

* String cmd = "PM Uninstall" + PackageName; or * String cmd = "PM Uninstall" + PackageName + "here";

* String cmd = "pm Install" + Apk_path; or * String cmd = "PM Uninstall" + apk_path+ "here";

and the service and alarm timing of the application itself is automatically canceled after the data is clear, and the process ends, and so can all the ADB commands.


Package Com.app.exit.demo;


Import java.io.IOException;


Import Android.util.Log;


/**
* Run Command Manager
* Equivalent to executing commands after the PC-side adb shell, PM clear PackageName
* But can only go back to their own operations, can not erase other applications of data;
* Uninstall other apps (root device or vendor-supplied interface)
* @author Hulk
*
*/
public class Runtimecmdmanager {


private static final String TAG = "Runtimecmdmanager";


/**
* Clear the app cache user data while stopping all services and alarm timed task
* String cmd = "pm Clear" + PackageName;
* String cmd = "pm Clear" + PackageName + "here";
* Runtime.getruntime (). EXEC (CMD)
* @param packagename
* @return
*/
public static Process Clearappuserdata (String packagename) {
Process p = execruntimeprocess ("PM Clear" + packagename);
if (p = = null) {
LogTag.log ("Clear app data packagename:" + packagename
+ ", FAILED!");
} else {
LogTag.log ("Clear app data packagename:" + packagename
+ ", SUCCESS!");
}
return p;
}


/**
* Uninstall app, only valid for root device
* String cmd = "PM Uninstall" + PackageName;
* Runtime.getruntime (). EXEC ("PM Uninstall" + PackageName)
* @param packagename
* @return
*/
public static Process Uninstallapp (String packagename) {
Process p = execruntimeprocess ("PM Uninstall" + PackageName);
if (p = = null) {
LogTag.log ("Uninstall app PackageName:" + PackageName + ", FAILED!");
} else {
LogTag.log ("Uninstall app PackageName:" + PackageName + ", SUCCESS!");
}
return p;
}


public static Process execruntimeprocess (String commond) {
Process p = null;
try {
p = runtime.getruntime (). exec (Commond);
} catch (IOException e) {
LogTag.log ("Exec Runtime commond:" + Commond + ", IOException" + e);
E.printstacktrace ();
}
LogTag.log ("Exec Runtime commond:" + Commond + ", Process:" + P);
return p;
}
}


To completely exit the application, do not clear the data method:

Stop all service in the app. Call Activitymanager.killbackgroundprocesses (PackageName);

Example:

Package Com.app.exit.demo;


Import java.util.List;


Import android.support.v7.app.ActionBarActivity;
Import Android.app.ActivityManager;
Import Android.app.AlarmManager;
Import android.app.PendingIntent;
Import Android.app.ActivityManager.RunningServiceInfo;
Import Android.content.Context;
Import android.content.Intent;
Import Android.content.pm.PackageInfo;
Import Android.content.pm.PackageManager;
Import Android.content.pm.ServiceInfo;
Import Android.os.Bundle;
Import Android.util.Log;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.TextView;


public class Mainactivity extends Actionbaractivity {


private static final String TAG = "mainactivity";
public static final int alarm_interval_millis = 10 * 1000;


TextView MMSGTV;
Button exitappbtn;


List<runningserviceinfo> runningservices;
Activitymanager Mactivitymanager;
List<packageinfo> PackageInfos;
Serviceinfo[] Serviceinfos;
Alarmmanager Malarmmanager;
Private pendingintent operation;


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
MMSGTV = (TextView) Findviewbyid (R.ID.MSG_TV);
EXITAPPBTN = (Button) Findviewbyid (R.id.btn_exit_app);
Exitappbtn.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
Finish ();
}
});
LOGTAG.I ("" + this);
Malarmmanager = (Alarmmanager) getsystemservice (Context.alarm_service);
Mactivitymanager = (Activitymanager) getsystemservice (Activity_service);
Startservices ();
Runningservices = Mactivitymanager.getrunningservices (10000);
Proccessrunningservices (FALSE);
PackageInfos = Getpackagemanager (). Getinstalledpackages (
Packagemanager.get_services);
Processpackageinfos (FALSE);
Setrepeating ();
}


/**
* Start secondactivity regularly, clear your data or uninstall
*/
private void setrepeating () {
if (operation = = null) {
Operation = Getsecondpendingintent ();
}
Long startTime = System.currenttimemillis () + Alarm_interval_millis;
Malarmmanager.setrepeating (Alarmmanager.rtc_wakeup, StartTime,
Alarm_interval_millis, operation);
}


Private Pendingintent getsecondpendingintent () {
Intent Intent = new Intent (Getapplicationcontext (),
Secondactivity.class);
Pendingintent operation = Pendingintent.getactivity (
Getapplicationcontext (), 0, intent, 0);
return operation;
}


private void Startservices () {
StartService (New Intent (this, myservice1.class));
StartService (New Intent (this, myservice2.class));
}


private void Processpackageinfos (Boolean isstop) {
for (PackageInfo Pinfo:packageinfos) {
if (Getpackagename (). Equals (Pinfo.packagename)) {
Serviceinfos = pinfo.services;
for (ServiceInfo Sinfo:serviceinfos) {
if (isstop) {
Boolean result = Stoppackageservice (this,
Getpackagename (), sinfo.name);
LOG.W (TAG, "Stop sinfo.name:" + sinfo.name
+ ", result=" + result);
} else {
LOG.D (TAG, "sinfo.name:" + sinfo.name);
}
}
}
}
}


private void Proccessrunningservices (Boolean isstop) {
if (runningservices = = null) {
LOG.W (TAG, "runningservices is NULL!!");
Return
}


for (Runningserviceinfo runningserviceinfo:runningservices) {
String serviceName = RunningServiceInfo.service.getClassName ();
String process = runningserviceinfo.process;
if ("Com.app.exit.demo". Equals (process)) {
if (isstop) {
Boolean result = Stoppackageservice (Getapplication (),
Getpackagename (), serviceName);
LOG.W (TAG, "StopService" + ServiceName + ", result="
+ result);
} else {
LOG.I (TAG, "runningserviceinfo.process=" + process
+ ", runningserviceinfo.pid="
+ Runningserviceinfo.pid
+ ", runningserviceinfo.getclass=" + serviceName);
}
}
}
}


Private Boolean Stoppackageservice (context context, String PackageName,
String serviceName) {
Intent service = new Intent ();
Service.setclassname (PackageName, serviceName);
return Context.stopservice (service);
}


@Override
protected void OnDestroy () {
Super.ondestroy ();
KillProcess ();
LOGTAG.I ("OnDestroy:" + this);
}

private void KillProcess () {
Proccessrunningservices (TRUE);
Processpackageinfos (TRUE);
String PackageName = Getpackagename ();
Mactivitymanager.killbackgroundprocesses (PackageName);
}


@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}


@Override
public boolean onoptionsitemselected (MenuItem item) {
Handle Action Bar Item clicks here. The Action Bar would
Automatically handle clicks on the Home/up button, so long
As you specify a the parent activity in Androidmanifest.xml.
int id = item.getitemid ();
if (id = = r.id.action_settings) {
return true;
}
return super.onoptionsitemselected (item);
}
}

Note Manifest configuration:


<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.app.exit.demo"
Android:versioncode= "1"
Android:versionname= "1.0" >


<uses-sdk
Android:minsdkversion= "8"
Android:targetsdkversion= "/>"


<uses-permission android:name= "Android.permission.KILL_BACKGROUND_PROCESSES"/>


<application
Android:allowbackup= "true"
android:icon= "@drawable/ic_launcher"
Android:label= "@string/app_name"
Android:theme= "@style/apptheme" >
<activity
Android:name= ". 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>
<activity
Android:name= ". Secondactivity "
Android:label= "Secondactivity"
Android:launchmode= "Singletask" >
</activity>


<service android:name= ". MyService1 "/>
<service android:name= ". MyService2 "/>
</application>


</manifest>




Android to completely exit the app/erase data/Uninstall Application method

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.