繁体   English   中英

如何在android oreo和更高版本的设备中重新启动应用程序?

[英]how to restart app in android oreo and higher version devices?

我可以在低于android oreo的设备中重新启动应用程序,我的问题是,如何使用Kotlin或Java在oreo和更高版本的设备中执行重新启动?

val intent = Intent(applicationContext, MainActivity::class.java)
    val mPendingIntentId = 1000
    val mPendingIntent = PendingIntent.getActivity(applicationContext, mPendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT)
    val mgr = applicationContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent)
    System.exit(0)

在您的Activity调用中, recreate(); 这将导致在新实例中重新创建活动。

文档

System.exit(0)并不是最好的方法,因为您无法期望应用程序下正在运行的当前线程的状态,它可能导致文件损坏。 试试这个:

Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

Runtime.getRuntime().exit(0);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM