android source

App restart

리오파파 2025. 8. 15. 13:34

아예 새로 app을 시작하는 법

 

package beautiful.life.saychat.funcs;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public final class AppReStart {

    private AppReStart() {}

    public static void trigger(Context context) {
        // Use a descriptive tag for logging this critical event.
        String logTag = "AppReStart";
        Log.e(logTag, "RESTARTING APPLICATION ");

        // 1. Get the launch intent for the app's main activity.
        Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
        if (intent == null) {
            Log.e(logTag, "Could not find launch intent for package. Cannot restart.");
            // As a last resort, terminate the process without a restart.
            System.exit(1);
            return;
        }

        // 2. Create a PendingIntent to be triggered by the AlarmManager.
        int pendingIntentId = 123456; // A unique ID.
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                pendingIntentId,
                intent,
                PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE
        );

        // 3. Get the AlarmManager and schedule the restart.
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        // Set the alarm to trigger after a short delay (e.g., 100ms) to allow current operations to cease.
        alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent);

        // 4. Terminate the current application process. The Alarm will wake it up.
        Log.e(logTag, "Terminating process now.");
        System.exit(0);
    }
}

'android source ' 카테고리의 다른 글

kakaotalk text에서 stock 추리기  (0) 2025.08.15
bluetooth detect  (1) 2025.08.15
Screen On 상태 확인  (0) 2025.07.20
Cache Clear  (1) 2025.07.13
scheduler 로 task repeating  (1) 2025.07.08