use
Used for reset function implementation within the APP.
Solution 1: Use the command line pm clear package name to clear App data at the system level
advantage
It is consistent with the operation of clearing all data in the task manager, and will delete all APP data. Re-entering the APP requires re-applying permissions.
shortcoming
The system will directly kill the APP process and cannot pull up the APP.
Code
public static Process clearAppUserData(String packageName) { Process p = execRuntimeProcess("pm clear " + packageName); return p; } public static Process execRuntimeProcess(String commond) { Process p = null; try { p = ().exec(commond); } catch (IOException e) { (); } return p; }
Solution 2: Manually delete internal storage and external storage
advantage
By control by yourself, you can re-pull the APP.
shortcoming
1. It is not as thorough as the system-level clearance, for example, you do not need to re-apply for permission after pulling it up again. Of course, all databases and SharePreferences related will be deleted.
2. Because the database file is deleted, the current process needs to be deleted when the startup page is re-pulled, and the screen will be black at this time.
Code
Note: Explain that the () in the code below actually obtains the Application object of the current app, and can also be replaced with a context.
public void use(){ //use clearPublic(); clearPublic(); restartApp(); } public static void restartApp() { Activity activity = getActivity(); final Intent intent = ().getLaunchIntentForPackage(()); if (intent != null) { (Intent.FLAG_ACTIVITY_CLEAR_TOP); (intent); } //Kill the previous process (()); } /** * Clear public directory */ public static void clearPublic() { if (() == null) { throw new RuntimeException("App no init"); } String publicFilePath = ().getPath() + "/" + getPackageInfo().packageName; File dir = new File(publicFilePath); File[] files = (); if (null != files) { for (File file : files) { deleteFolder(()); } } } /** * Clear private directory */ public static void clearPrivate() { if (() == null) { throw new RuntimeException("App no init"); } //Clear the folder File dir = new File(().getFilesDir().getParent()); File[] files = (); if (null != files) { for (File file : files) { if (!().contains("lib")) { deleteFolder(()); } } } } /** * Delete the specified file */ private static boolean deleteDirectory(String filePath) { boolean flag = false; if (!()) { filePath = filePath + ; } File dirFile = new File(filePath); if (!() || !()) { return false; } flag = true; File[] files = (); for (File file : files) { if (()) { flag = deleteSingleFile(()); if (!flag) { break; } } else { flag = deleteDirectory(()); if (!flag) { break; } } } if (!flag) { return false; } return (); } /** * Delete a single file * * @param filePath Filename of deleted file * @return File deletion returns true, otherwise return false */ private static boolean deleteSingleFile(String filePath) { File file = new File(filePath); if (() && ()) { return (); } return false; } /** * Delete the specified directory or file according to the path, whether present or not */ private static boolean deleteFolder(String filePath) { File file = new File(filePath); if (!()) { return false; } else { if (()) { return deleteSingleFile(filePath); } else { return deleteDirectory(filePath); } } } /** * Get package information */ private static PackageInfo getPackageInfo() { PackageManager packageManager = ().getPackageManager(); PackageInfo packInfo = null; try { packInfo = (().getPackageName(), 0); } catch ( e) { (); } return packInfo; }
Summarize
This is the end of this article about two solutions for Android to completely clear APP data. For more related content about Android to completely clear APP data, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!