5

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

piks
  • 1,621
  • 8
  • 32
  • 59

4 Answers4

7

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
}
Satheesh
  • 10,998
  • 6
  • 50
  • 93
6

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.

Rajas47Ashtikar
  • 583
  • 2
  • 6
  • 11
  • Thank you. This should be accepted answer. More info at https://medium.com/@_foso_/what-is-a-managespaceactivity-on-android-26530ba4117b –  Feb 08 '20 at 03:40
3

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???

There is none. AFAIK, pressing Clear Data also stops your process, so your code will not be running. You would treat the user pressing Clear Data as being identical to the case where the user installed your app for the very first time.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • yeah true, my assumption is also the same but i am facing one strange problem in samsung galaxy tab: when any alarm comes from server my app will catch it and start playing wav file to notify to the user but at the same time if user goes to Application's info and click Clear data button, it should stop Audio track but it is not happening so if your statement is true the service should also get stopped means onDestroy shud get called on service where audio track is stopped but its not happening in this scenario. why? – piks May 23 '12 at 04:33
  • @piks: "it should stop Audio track" -- not necessarily. It should keep playing so long as it has access to the data. Partly, that will be in the form of buffers. It is also possible that the file will not be immediately deleted if the file is open by another process, as would be the case with media playback. – CommonsWare May 23 '12 at 10:13
  • 1
    But I am facing this issue only on devices which is having Android 2.2(Froyo) installed like Samsung Galaxy tab and if I am repeating this scenario in other version of android 2.1 ,2.3 and above then upon clicking on Clear Data button, beep sound is also getting stopped. – piks May 24 '12 at 10:31
  • @piks: You are testing on a few devices. There are hundreds of devices. The behavior of `MediaPlayer`, `SoundPool`, etc. when "Clear Data" is clicked is undocumented. You should not be making *any* assumptions about what will happen. – CommonsWare May 24 '12 at 11:12
  • and whats the solution for widgets? Because if you have a widget with info in it, but you clear data, that info is no longer useful. Do we have to wait for the widget to refresh? looks like bug... widgets should reset on clear data i guess... – nsL Sep 18 '14 at 21:15
  • @nsL: "Do we have to wait for the widget to refresh?" -- I haven't tried Clear Data on an app with an outstanding app widget. Assuming the app widget survives (and it could be removed, for all I know), your app won't know that it exists until the next update request, in all likelihood. – CommonsWare Sep 18 '14 at 22:02
-1

Perhaps set up a broadcast receiver in your Manifest and listen for the

"android.intent.action.PACKAGE_DATA_CLEARED"

Intent that is broadcast when an application's data is cleared from settings. You should be able to find out more information about this on the android developers website.

http://developer.android.com/reference/android/content/Intent.html#ACTION_PACKAGE_DATA_CLEARED

SavageKing
  • 556
  • 4
  • 16
  • 5
    This will not work, quoting Android docs here, "Note that the cleared package does not receive this broadcast." – Satheesh Feb 23 '16 at 06:16