Android - 清除应用程序数据并重启设备

时间:2016-12-05 09:43:46

标签: android cordova powermanager

我正在使用cordova编写Android应用程序。此应用仅安装在专用的Android 5.1.1设备上。除此之外,我还具有清除所有应用数据的功能。我已经在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);
  }
}

我想在删除所有应用数据完成后重启设备。这是我的reboot-function:

private void rebootDevice(){
  Context mContext = cordova.getActivity().getApplicationContext();
  PowerManager pManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
  pManager.reboot(null);
}

重启功能本身正在运行。但是我遇到了问题,当我拨打((ActivityManager)cordova.getActivity().getApplicationContext().getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();时它没有达到此功能,因为应用程序会立即关闭。

我该如何解决这个问题?如何清除应用程序数据并重启设备?

1 个答案:

答案 0 :(得分:0)

我去了this solution

我的插件代码:

public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
  if (action.equals("factory_reset")) {
    clearApplicationData();
    rebootDevice();
  }
}

私有函数,称为:

private void clearApplicationData() {
  File cache = cordova.getActivity().getApplicationContext().getCacheDir();
  File appDir = new File(cache.getParent());
  Log.d(TAG, "AppDir = " + appDir);
  if (appDir.exists()) {
    String[] children = appDir.list();
    for (String s : children) {
      if (!s.equals("lib")) {
        Log.d(TAG, "Delete " + s);
        deleteDir(new File(appDir, s));
      }
    }
  }
}

private void rebootDevice(){
  Context mContext = cordova.getActivity().getApplicationContext();
  PowerManager pManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
  pManager.reboot(null);
}
相关问题