Android completely exits the application, clears data, and detaches the application.
Clear application data and uninstall the application, and directly execute the command:
* 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 ";
After the data is clear, the Service and Alarm of the application are automatically canceled and the process ends. Similarly, all commands of adb can be executed in this way.
Package com. app. exit. demo;
Import java. io. IOException;
Import android. util. Log;
/**
* Run the command manager.
* Equivalent to executing the command after the PC adb shell, pm clear packageName
* You can only perform operations on your own, but cannot clear data from other applications;
* Uninstall other applications (limited to root devices or interfaces provided by the vendor)
* @ Author hulk
*
*/
Public class RuntimeCmdManager {
Private static final String TAG = "RuntimeCmdManager ";
/**
* Clears user data cached by the application and stops all services and Alarm scheduled tasks.
* String cmd = "pm clear" + packageName;
* String cmd = "pm clear" + packageName + "HERE ";
* Runtime.getruntime(cmd.exe c (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 the application, which is only valid for the root device.
* String cmd = "pm uninstall" + packageName;
* Runtime.getruntime(cmd.exe c ("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(cmd.exe c (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:
Stop all services in the application. 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 RunningServices;
ActivityManager mActivityManager;
List 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 to clear or uninstall data.
*/
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 will
// Automatically handle clicks on the Home/Up button, so long
// As you specify a parent activity in AndroidManifest. xml.
Int id = item. getItemId ();
If (id = R. id. action_settings ){
Return true;
}
Return super. onOptionsItemSelected (item );
}
}
Note the Manifest Configuration:
Package = "com. app. exit. demo"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
Android: minSdkVersion = "8"
Android: targetSdkVersion = "21"/>
Android: allowBackup = "true"
Android: icon = "@ drawable/ic_launcher"
Android: label = "@ string/app_name"
Android: theme = "@ style/AppTheme">
Android: name = ". MainActivity"
Android: label = "@ string/app_name">
Android: name = ". SecondActivity"
Android: label = "SecondActivity"
Android: launchMode = "singleTask">