Android programming realizes application restart

A sub-function can be written separately and then called as an interface.
The specific code is as follows:

public void ReLogin(Activity activity){
    
    
        Intent intent = activity.getPackageManager()
                .getLaunchIntentForPackage(activity.getApplication().getPackageName());
        PendingIntent restartIntent = PendingIntent.getActivity(LoginActivity.loginActivity, 0, intent, 0);
        AlarmManager mgr = (AlarmManager)activity.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, restartIntent); // 1秒钟后重启应用
        System.exit(0);
    }

Note:
1. The parameter passed in is Activity;
2. "LoginActivity.loginActivity" in the code is the Activity to be run after the application is restarted.
——————————————————————————————
Finally, post my personal public account: WeChat search "Chaqian" or scan the picture below. Usually, some programming-related articles will be updated, and everyone is welcome to pay attention~
tea move

Guess you like

Origin blog.csdn.net/weixin_46269688/article/details/110367803