Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to capture the Clear Data action programmatically in application?

Tags:

android

I developed one android app which is having sets of activities and one background service running. I am keeping some app data to the device's internal memory so when users clicks on Clear Data option from Settings->Applications-> Clear Data button, all data saved in internal memory gets cleared. I have to captured Clear Data click event or action(if any available) when user clicks on Clear Data button in my App's service OR in broadcast receiver class, can anyone suggest that is there any action thrown by device upon clicking on Clear data of app so I could catch that action in my app broadcast receiver class and perform desired task???

Please provide me the resolution with some example.

Regards, Piks

like image 858
piks Avatar asked May 22 '12 10:05

piks


2 Answers

The best way would be storing a value in SharedPreferences like "IS_DATA_CLEARED" when the app is run first or when you save all required details and always make sure this value is never fiddled with at any time during the app's life cycle.

SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = appSharedPrefs.edit();
editor.putBoolean("IS_DATA_CLEARED",false);
editor.apply();

Then every time do the below check.

SharedPreferences sharedPreferences = PreferenceManager
            .getDefaultSharedPreferences(this);
boolean IS_DATA_CLEARED =  appSharedPrefs.getBoolean("IS_DATA_CLEARED",true);

if(IS_DATA_CLEARED){
  //DATA IS CLEARED
}
else{
  //DATA IS NOT CLEARED
}
like image 94
Satheesh Avatar answered Nov 13 '22 05:11

Satheesh


What you can do is use the following attribute in the application tag in the Manifest File.

android:manageSpaceActivity=".path.to.MyActivity"

This will replace the button "Clear Data" from the settings to "Manage Space". Now you can redirect the user to a custom activity that is controlled by you.

The launch of the activity will act as callback for you. Manipulate your data in your activity and use the following code

private void clearPreferences() {
    try {
        // clearing app data
        Runtime runtime = Runtime.getRuntime();
        runtime.exec("pm clear YOUR_APP_PACKAGE_GOES HERE");

    } catch (Exception e) {
        e.printStackTrace();
    }
   }

to clear the app data.

like image 25
Rajas47Ashtikar Avatar answered Nov 13 '22 07:11

Rajas47Ashtikar