IT技术安卓硬件

Android之app重启功能实现

2018-11-29  本文已影响37人  肚皮怪_Sun

记一个小功能重启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(i);
上一篇下一篇

猜你喜欢

热点阅读