Skip to content

Commit

Permalink
A better restart
Browse files Browse the repository at this point in the history
  • Loading branch information
shatyuka committed Apr 3, 2024
1 parent 795b641 commit 093cd8b
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions app/src/main/java/com/shatyuka/zhiliao/Helper.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.shatyuka.zhiliao;

import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
Expand Down Expand Up @@ -118,21 +116,15 @@ public static boolean getDarkMode() {
return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
}

@SuppressLint("UnspecifiedImmutableFlag")
public static void doRestart(Context context) {
try {
if (context != null) {
PackageManager pm = context.getPackageManager();
if (pm != null) {
Intent mStartActivity = pm.getLaunchIntentForPackage(context.getPackageName());
if (mStartActivity != null) {
mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent mPendingIntent = PendingIntent.getActivity(context, 0, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
AlarmManager mgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
}
}
PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(context.getPackageName());
if (launchIntent != null) {
Intent restartIntent = Intent.makeRestartActivityTask(launchIntent.getComponent());
restartIntent.setPackage(context.getPackageName());
context.startActivity(restartIntent);
System.exit(0);
}
} catch (Exception ignored) {
}
Expand Down

0 comments on commit 093cd8b

Please sign in to comment.