我需要我的应用程序重新启动时点击一个按钮,更多信息我需要模拟,我推回钥匙,再次打开应用程序。 我认为我可以使用finish();
关闭应用程序,但我怎么能再次启动onCrete()
? 有没有其他方法可以做到这一点?重新启动应用程序,当我点击一个按钮
谢谢
我需要我的应用程序重新启动时点击一个按钮,更多信息我需要模拟,我推回钥匙,再次打开应用程序。 我认为我可以使用finish();
关闭应用程序,但我怎么能再次启动onCrete()
? 有没有其他方法可以做到这一点?重新启动应用程序,当我点击一个按钮
谢谢
使用意图调用您的活动。它会再次启动活动。
Intent intent = new Intent(MyActivity.this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //this will always start your activity as a new task
startActivity(intent);
您还可以添加,android:launchMode="singleTask"
内您的清单,以便只有一个你的活动实例将保持在任何时候。
同样的事情here. :)
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
要重新启动您的应用程序,你可以使用报警管理器,使您的活动开你退出程序后。
AlarmManager alm = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
alm.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0));
android.os.Process.sendSignal(android.os.Process.myPid(), android.os.Process.SIGNAL_KILL);
startActivity()with do your job .. – NullPointerException