Clear Data and Cache

Before I uninstall an app, I need to clear all the data and cache of this app. And the easiest way to clear cache is one line of ADB command. It’s also the same if you go to the settings and find the app, click “Clear data”.

adb shell pm clear com.meizu.net.search

Clear Data and Cache

Clear RAM

Kill Processes

  1. Add permission in AndroidManifest.xml

     <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
    
  2. Kill background processes whose relative importance level is better than IMPORTANCE_VISIBLE (200). More information about importance.

     private void clearRAM() {
         ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
         List<RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
    
         if (infos != null) {
             for (RunningAppProcessInfo info : infos) {
                 if (info.importance > RunningAppProcessInfo.IMPORTANCE_VISIBLE) {
                     String[] pkgList = info.pkgList;
                     for (String pkg : pkgList) {
                         try {
                             manager.killBackgroundProcesses(pkg);
                         } catch (Exception e) {
                             e.printStackTrace();
                         }
                     }
                 }
             }
         }
     }
    

Stop Services

After I kill processes, the services are still running in the background, and some of them can restart the processes. So it seems the services have to be stopped too. I found a solution from Stack Overflow, but it seems I have to modify the system file, so I just give up.

After using

activitymanager.killbackgroundprocesses(packageName)

we have to check if there are any services running under that package name or process name.

Then we have to use

stopService(new Intent().setComponent(serviceInfos.get(i).service))

But it would return an error because android does not allow us to stop another service

So I used these two lines in the manifest and then ran my app in the build machine

coreApp="true"
android:sharedUserId="android.uid.system"

These two lines are there in the android settings manifest file, which allows settings to stop service if the user wants.

So the activity got killed along with the service… :)

And remember this worked only on the build machine….

– DSP

Then I thought about force-stop, it not only stops services but also the entire app. It works in my task manager app, but needs root access. If you have a better solution without root access, live your comment.

public void forceStop(String packageName) {
    java.lang.Process process = null;
    DataOutputStream os = null;
    try {
        process = Runtime.getRuntime().exec("su");
        os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("am force-stop " + packageName + "\n");
        os.flush();
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (os != null)
                os.close();
            if (process != null)
                process.destroy();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Clear Memory Card

  1. Add permission in AndroidManifest.xml

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
  2. Because I need clear entire memory card, I use recurrence algorithm to delete every file.

     private void clearMemoryCard() {
         try {
             wipeDirectory(Environment.getExternalStorageDirectory().toString());
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
    
     private void wipeDirectory(String name) {
         File directoryFile = new File(name);
         File[] files = directoryFile.listFiles();
         if (files != null) {
             for (File file : files) {
                 if (file.isDirectory()) {
                     wipeDirectory(file.toString());
                     file.delete();
                 } else {
                     file.delete();
                 }
             }
         }
     }