2017-04-05 2 views
1

Androidアプリケーションのすべてのユーザー/キャッシュデータを削除する必要がある機能を実装する必要があります。以下、このコードを実行し、簡単なプッシュ:"pm clearコマンド"を使用して終了した後にアプリケーションを再起動する方法

String performClearData() throws JSONException { 
    JSONObject jdata = new JSONObject(); 
    try { 
     FilesUtilities fu = new FilesUtilities(); 
     fu.deleteFiles(); 

     Runtime runtime = Runtime.getRuntime(); 
     runtime.exec("pm clear MY.APP"); 
     jdata.put("Stato del comando", "Eseguito"); 
    } catch (Exception e) { 
     wil.WriteFile("warning", "5)PushResponderEngine - Exception in PerformClearData: " + e.toString()); 
    } 
    return jdata.toString(); 
} 

ことが可能、このコマンドの後にアプリケーションを再起動した場合、今私は知っているだろう、私はこのコードを試してみましたが、動作しません:

intent = PendingIntent.getActivity(MYApplication.getInstance().getBaseContext(), 0,new 
Intent(getIntent()), getIntent().getFlags()); 
    AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent); 
    System.exit(2); 
+0

上記のコードでは、Thread.setDefaultUncaughtExceptionHandler()に提供されている関数に設定する必要があります(例:https://www.tutorialspoint.com/java/lang/thread_setdefaultuncaughtexhandler.htm – Alexandre

+0

)申し訳ありませんが、私はこのコードで何をしなければならないか理解しています... 一例を挙げてその概念を説明してください。 ありがとう – Max

答えて

1

編集:

public class StartActivity extends Activity { 

final Context context; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.context = context; 
    setContentView(R.layout.start_activity); 
    Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { 
     public void uncaughtException(Thread t, Throwable e) { 
      StartActivity.restartApp(context); 
     } 
    }); 
    //your code here 
} 

public static void restartApp(Context context) 
{ 
    Intent mStartActivity = new Intent(context, StartActivity.class); //Replace StartActivity with the name of the first activity in your app 
    int mPendingIntentId = 123456; 
    PendingIntent mPendingIntent = PendingIntent.getActivity(context, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); 
    AlarmManager mgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); 
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); 
    System.exit(0); 
} 

} 

あなたはアンドロイドの午後明確なデータコマンドを呼び出すと、あなたは(アプリケーションを閉じる強制:あなたのStartActivity.javaで

、あなたはこのようなのonCreate()メソッドでThread.setDefaultUncaughtExceptionHandler(ハンドラ)を設定する必要があります。それが開かれている場合)。 when the app gets force closed, it throws an unhandledException、これは上記のコードで処理されます。

+0

問題は、アプリケーションを再起動しません。アプリケーションを強制終了した後、アプリケーションを再起動します。 – Max

+0

adbシェルからpmコマンドを実行していますか? – Alexandre

+0

いいえ、クラス内の関数にあります。私は単純なプッシュを使って "呼び出す"ことができる機能を実装しました – Max

関連する問題