올해는 머신러닝이다.
Android 13 이상에서 부팅 후 앱 실행하는 방법 본문
Android 13(API 33) 이상에서는 앱이 BOOT_COMPLETED 브로드캐스트를 수신하여 자동 실행하는 기능이 제한됩니다. 즉, 기존의 BOOT_COMPLETED를 사용하는 방식이 기본적으로 작동하지 않을 가능성이 높습니다. 하지만 몇 가지 방법을 활용하면 여전히 앱을 재부팅 후 실행할 수 있습니다.
✅ Android 13 이상에서 부팅 후 앱 실행하는 방법
1️⃣ 기본적인 BOOT_COMPLETED 사용 가능 여부
- Android 13(API 33) 이상에서도 BOOT_COMPLETED 사용은 가능하지만, 앱이 백그라운드 제한을 받지 않는 경우에만 정상 동작합니다.
- 즉, 앱이 사용자가 직접 설치하고, 최소 1회 실행한 경우, BOOT_COMPLETED 브로드캐스트를 받을 수 있습니다.
- 제한 사항
- 백그라운드 실행이 제한된 앱은 BOOT_COMPLETED를 받을 수 없음
- 앱이 "사용자 실행 앱"으로 분류되지 않으면 작동하지 않을 가능성이 있음
2️⃣ 대체 방법: ActivityManager와 AlarmManager 활용
Android 13 이상에서는 BOOT_COMPLETED 대신, 사용자가 기기를 다시 켠 후 특정 시점에 앱을 실행하도록 예약하는 방식이 더 적합합니다.
📌 AlarmManager를 사용하여 부팅 후 앱 실행 예약
앱을 백그라운드 제한 없이 실행할 수 있도록 예약하는 방법입니다.
package com.example.myapp;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
// 앱 실행을 예약
scheduleAppLaunch(context);
}
}
private void scheduleAppLaunch(Context context) {
Intent launchIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(
context, 0, launchIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.setExact(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 5000, // 5초 후 실행
pendingIntent
);
}
}
}
🔹 장점:
- BOOT_COMPLETED가 막혀도 일정 시간 후 앱이 실행됨
- AlarmManager를 활용하면 앱이 자동으로 시작됨
🔹 단점:
- 완전한 즉시 실행이 아닌, 특정 지연 시간 후 실행됨
- 일부 기기에서는 절전 모드로 인해 정확한 실행이 보장되지 않을 수도 있음
3️⃣ Foreground Service 활용
Android 13에서는 백그라운드 제한이 많기 때문에, 부팅 후 Foreground Service를 사용하여 앱을 실행하는 것도 가능성이 있습니다.
public class BootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent serviceIntent = new Intent(context, MyForegroundService.class);
context.startForegroundService(serviceIntent);
}
}
}
이렇게 하면 앱이 부팅 후 실행될 가능성이 더 높아집니다.
단, Foreground Service 사용을 위해 AndroidManifest.xml에서 FOREGROUND_SERVICE 권한을 추가해야 합니다.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
🔥 최종 결론
- Android 13(API 33) 이상에서는 BOOT_COMPLETED만으로 앱 실행이 어려움
- 앱이 사용자에 의해 실행된 기록이 있다면 BOOT_COMPLETED 사용 가능
- AlarmManager를 활용하여 부팅 후 일정 시간 후 앱을 실행하는 방법이 효과적
- Foreground Service를 활용하면 좀 더 강제적으로 실행 가능하지만, 배터리 제한이 있을 수 있음
- 기기에 따라 동작 방식이 다를 수 있으며, 일부 제조사(삼성, 샤오미 등)는 추가적인 백그라운드 제한을 둘 수 있음
📌 추천 방법 (Android 13 이상)
✅ 1. BOOT_COMPLETED + AlarmManager 방식으로 앱 실행 예약
✅ 2. Foreground Service를 사용하여 앱 실행
✅ 3. WorkManager를 활용하여 백그라운드에서 앱 실행 가능하도록 설정
위 방법을 조합하면 Android 13 이상에서도 부팅 후 앱 실행이 가능합니다.
'스터디 > Android' 카테고리의 다른 글
Android 카메라 줌 관련 정리 (0) | 2025.02.07 |
---|---|
Android 스크린샷 adb를 통해서 가져오기 (0) | 2018.10.31 |
[Android] 배달앱 클론 3주차 수업 정리 (0) | 2018.10.21 |
[Android] 배달앱 클론 스터디 2주차 (0) | 2018.10.14 |
Android Webview FileUpload os별 설정 (0) | 2018.10.10 |