0

我正在用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);
  }
}

在删除所有应用程序数据完成后,我想重新启动设备。这是我的重启功能:

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();因为应用程序立即强制关闭。

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

4

1 回答 1

0

我选择了这个解决方案

我的插件代码:

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);
}
于 2017-02-07T15:13:13.270 回答