배터리 최적화 모드 제외

Eungi Kim·2020년 11월 17일
0

A 앱에서 B 앱의 서비스를 스타트 시키는데 foreground service 가 아닐 경우 에러가 난다.

배터리 최적화 모드에서 제외시키면 동작 가능하다.

  1. 배터리 사용량 최적화 OS 설정화면으로 이동하여 사용자가 직접 앱 찾아서 설정

최적화하지 않은 앱 에서 전체를 선택한 다음 자기 앱을 찾고 버튼을 눌러줘야 한다.

Intent intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
startActivity(intent);
  1. 다얄로그 띄우고 확인으로 설정 변경

필요 권한

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

해당 권한 명시시 Google play 에 등록이 거부됨. 특별한 기능이 있는 경우 설명 후 등록 하능하다.

Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + packageName));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

이렇게 될 경우 앱 내에서 확인/취소 다얄로그가 띄워지고 설정 변경 가능하다.

  1. 배터리 최적화 모드 설정 여부
PowerManager powerManager= (PowerManager) getSystemService(Context.POWER_SERVICE);
boolean isIgnore = powerManager.isIgnoringBatteryOptimizations(getPackageName());

https://developer.android.com/training/monitoring-device-state/doze-standby#whitelisting-cases

profile
Run and gun and debugun

0개의 댓글