Android之app重启功能实现

记一个小功能重启app

方式一:使用AlarmManger

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
 //与正常页面跳转一样可传递序列化数据,在Launch页面内获得
intent.putExtra("REBOOT","reboot");
 PendingIntent restartIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
 AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
 mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent);
 android.os.Process.killProcess(android.os.Process.myPid());

方式二:通过设置FLAG_ACTIVITY_CLEAR_TOP

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//与正常页面跳转一样可传递序列化数据,在Launch页面内获得
intent.putExtra("REBOOT","reboot");
startActivity(intent);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容