1

I am trying to clear a data from within the app and my app is device owner, hence I am getting and error

java.lang.SecurityExeception :Clearing DeviceOwner data is forbidden.

Code I am using is

public void onClearData(View view) {

    try {
        boolean isCleared = ((ActivityManager) getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
        if (!isCleared) {
            Toast.makeText(this, "Not able to clear the data", Toast.LENGTH_SHORT).show();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Now, my question is that how it will be possible to clear a data of device owner app from within the app? Would appreciate a help.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Gunjan Dave
  • 161
  • 1
  • 2
  • 10

1 Answers1

0

The way you're doing it is how it's done, according to the docs.

But since you're getting that security exception, your app is probably set as a device owner app, and you're not allowed to deactivate it, remove its data nor uninstall it while it is on this state.

If that's really the case I'd suggest you to unset it as a Device Owner App. Try to use dpm remove-active-admin for that.

Take a look at those questions for more info:

Mauker
  • 11,237
  • 7
  • 58
  • 76
  • Thanks for the response. But I have already mentioned in my comment that my app is already and device owner and I want it to be device owner. So how can I clear the data of owner app from within itself? – Gunjan Dave Dec 20 '18 at 06:03
  • As it seems, you can't. You have to unset it as a device owner first before trying to remove your data. – Mauker Dec 20 '18 at 12:46