I am programming an Android-application with cordova. This app gets only installed on dedicated Android 5.1.1-devices. Among others I have the functionality to clear all the apps data. I have implemented this functionality in a cordova-plugin:
// My Cordova Plugin java
if (action.equals("factory_reset")) {
  try {
    Log.i(TAG, "Factory Reset");
    ((ActivityManager)cordova.getActivity().getApplicationContext().getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
    rebootDevice();
  } catch (Exception ex) {
    Log.w(TAG, "Error while doing a Factory Reset", ex);
  }
}
I want to reboot the device, after the deletion of all app-data is finished. Here is my reboot-function:
private void rebootDevice(){
  Context mContext = cordova.getActivity().getApplicationContext();
  PowerManager pManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
  pManager.reboot(null);
}
The reboot function itself is working. However I have the problem, that it doesn't reach this function when I call ((ActivityManager)cordova.getActivity().getApplicationContext().getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData(); because the app immediately get's force closed.
How can I solve this? How can I clear the application-data AND reboot the device?