美文网首页IT技术安卓硬件
Android之app重启功能实现

Android之app重启功能实现

作者: 肚皮怪_Sun | 来源:发表于2018-11-29 16:23 被阅读37次

    记一个小功能重启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);
    

    相关文章

      网友评论

        本文标题:Android之app重启功能实现

        本文链接:https://www.haomeiwen.com/subject/lfefcqtx.html