2016-12-05 149 views
0

我正在编程一个Android应用程序与科尔多瓦。此应用只能安装在专用Android 5.1.1设备上。其中我有清除所有应用程序数据的功能。我已经在科尔多瓦 - 插件来实现这个功能:Android - 清除应用程序数据和重新启动设备

// 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();时,它没有达到此功能,因为应用程序立即关闭。

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

回答

0

我去this solution

我Plugincode:

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); 
} 
相关问题