我想在清除应用程序数据后打开一个新活动


try {
// clearing app data
String packageName = getApplicationContext().getPackageName();
Runtime runtime = Runtime.getRuntime();
runtime.exec("pm clear "+packageName);

} catch (Exception e) {
e.printStackTrace();
}
Intent intent = new Intent(BaseActivity.this, SplashActivity.class);
startActivity(intent);
}

我想在点击按钮清除数据代码后打开新活动,上面是我的点击按钮代码,但当我运行它时,它会清除数据,但不会打开新活动。请帮忙提前感谢

您可以尝试以下代码。但它有一个限制,因为clearApplicationUserData((需要最低版本的SDK 19。

Intent intent = new Intent(BaseActivity.this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent);
System.exit(0);
((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).clearApplicationUserData();

最新更新