Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear Application's Data Programmatically

Tags:

android

I want to clear my application's data programmatically.

Application's data may contain anything like databases, shared preferences, Internal-External files or any other files created within the application.

I know we can clear data in the mobile device through:

Settings->Applications-> ManageApplications-> My_application->Clear Data

But I need to do the above thing through an Android Program?

like image 654
uday Avatar asked May 26 '11 05:05

uday


People also ask

How do I clear app data automatically?

Step #1: Download the Auto Clean Up App from Google Play Store. Step #2: Grant the Necessary Permission in order to use the application. Step #3: Grant the Permission of Usage Tracking in your android device to let the app scan for the cache and cookies stored in your device.

What is clear data in app Manager?

Clearing data essentially reset an app to its default state: it makes your app act like when you first downloaded and installed it. For example, say you make changes to the settings of your favorite fitness app.

How do I close a program in android programmatically?

use 'System. exit(0);' when you really want to exit the app.


2 Answers

There's a new API introduced in API 19 (KitKat): ActivityManager.clearApplicationUserData().

I highly recommend using it in new applications:

import android.os.Build.*; if (VERSION_CODES.KITKAT <= VERSION.SDK_INT) {     ((ActivityManager)context.getSystemService(ACTIVITY_SERVICE))             .clearApplicationUserData(); // note: it has a return value! } else {     // use old hacky way, which can be removed     // once minSdkVersion goes above 19 in a few years. } 

If you don't want the hacky way you can also hide the button on the UI, so that functionality is just not available on old phones.

Knowledge of this method is mandatory for anyone using android:manageSpaceActivity.


Whenever I use this, I do so from a manageSpaceActivity which has android:process=":manager". There, I manually kill any other processes of my app. This allows me to let a UI stay running and let the user decide where to go next.

private static void killProcessesAround(Activity activity) throws NameNotFoundException {     ActivityManager am = (ActivityManager)activity.getSystemService(Context.ACTIVITY_SERVICE);     String myProcessPrefix = activity.getApplicationInfo().processName;     String myProcessName = activity.getPackageManager().getActivityInfo(activity.getComponentName(), 0).processName;     for (ActivityManager.RunningAppProcessInfo proc : am.getRunningAppProcesses()) {         if (proc.processName.startsWith(myProcessPrefix) && !proc.processName.equals(myProcessName)) {             android.os.Process.killProcess(proc.pid);         }     } } 
like image 133
TWiStErRob Avatar answered Sep 21 '22 13:09

TWiStErRob


I'm just putting the tutorial from the link ihrupin posted here in this post.

package com.hrupin.cleaner;  import java.io.File;  import android.app.Application; import android.util.Log;  public class MyApplication extends Application {      private static MyApplication instance;      @Override     public void onCreate() {         super.onCreate();         instance = this;     }      public static MyApplication getInstance() {         return instance;     }      public void clearApplicationData() {         File cacheDirectory = getCacheDir();         File applicationDirectory = new File(cacheDirectory.getParent());         if (applicationDirectory.exists()) {             String[] fileNames = applicationDirectory.list();             for (String fileName : fileNames) {                 if (!fileName.equals("lib")) {                     deleteFile(new File(applicationDirectory, fileName));                 }             }         }     }      public static boolean deleteFile(File file) {         boolean deletedAll = true;         if (file != null) {             if (file.isDirectory()) {                 String[] children = file.list();                 for (int i = 0; i < children.length; i++) {                     deletedAll = deleteFile(new File(file, children[i])) && deletedAll;                 }             } else {                 deletedAll = file.delete();             }         }          return deletedAll;     } } 

So if you want a button to do this you need to call MyApplication.getInstance(). clearApplicationData() from within an onClickListener

Update: Your SharedPreferences instance might hold onto your data and recreate the preferences file after you delete it. So your going to want to get your SharedPreferences object and

prefs.edit().clear().commit(); 

Update:

You need to add android:name="your.package.MyApplication" to the application tag inside AndroidManifest.xml if you had not done so. Else, MyApplication.getInstance() returns null, resulting a NullPointerException.

like image 39
MinceMan Avatar answered Sep 18 '22 13:09

MinceMan