2016-12-05 26 views
0

私はcordovaで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); 
} 
関連する問題