DOZE모드에서 GPS정보 얻기

조회수 290회

안녕하세요? 혼자서만 코딩하다가 문제를 해결하지 못하고 있다가 개발자 커뮤니티가 있어 질문을 합니다.

저는 위치정보를 응답해주는 버스위치 확인용 앱을 개발하고 있습니다. 현재 오래된 안드로이드폰을 이용해 버스 위치를 알려주는 서비스를 운영중입니다. 안드로이드폰이 너무 오래되고 밧데리의 폭발 가능성도 있어서 이제 새로운 폰으로 교체하려고 합니다.

문제는 DOZE모드에서 FCM 메세지를 수신하여 GPS위치 확인하는 서비스를 호출합니다. 물론 startForegroundService로 서비스를 호출하는데, 서비스에서 GPS정보를 받기 위해 requestLocationUpdates를 호출하면 응답이 없습니다. 제 생각에는 정상적으로 포그라운드 서비스가 안되어서 그런 거 같은데, 좋은 의견 부탁합니다.

서비스 실행시 onCreate메서드 호출후 onStartCommand에서 아래처럼 처리했습니다.

public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);

    wakeLock.acquire(60*1000);      // 60초 뒤에 자동으로 해제
    apiClient.connect();

    return Service.START_STICKY;
}

참고로 onCreate()에서 아래의 startForegroundService() 메서드를 호출합니다.

protected void startForegroundService() {

    String channelId = "ShuttleBusServiceChannel";
    String channelName = "ShuttleBusServiceChannel";

    Intent notificationIntent = new Intent(this, MainActivity.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent pendingIntent = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
        pendingIntent = PendingIntent.getActivity
                (this, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
    } else {
        pendingIntent = PendingIntent.getActivity
                (this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
    }

    NotificationManager notifManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder builder;
    if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel mChannel = new NotificationChannel(channelId,
                channelName, NotificationManager.IMPORTANCE_DEFAULT);
        notifManager.createNotificationChannel(mChannel);
        builder = new Builder(this, channelId);
    } else {
        builder = new Builder(this);
    }
    builder.setContentTitle("위치안내")
            .setContentText("위치 통보 요청") 
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setSmallIcon(android.R.drawable.btn_dropdown)
            .setLargeIcon(BitmapFactory.decodeResource(getResources()
                    , R.drawable.ic_launcher_background))
            .setContentIntent(pendingIntent);

    startForeground(1, builder.build());
}

서비스 호출은 제대로 되는 거 같은데, 위치 정보 호출시 DOZE모드에서는 실행이 되는 건가요?

포그라운드서비스를 호출하면 백그라운드서비스가 중단되지 않는다고 들었는데, 위치정보를 수신할 수가 없네요. 테스트 삼아 서비스에서 메인엑티비티를 호출하니, 위치정보가 지속적으로 수집되네요.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)