마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

[고수님께] 백그라운드 서비스 호출 관련 오류 문의드립니다.

0 추천

아주 오래된 오류 하나 문의합니다.


포그라운드서비스 호출 관련 오류입니다.
오류내용은 다음과 같아요
Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()


매니페스트에 아래권한 추가되어 있구요.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />



*ManageService.class 서비스 클래스 코드 

@Override
public void onCreate() {
 
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     int 
NOTIFICATION_ID = 2020;
     String CHANNEL_ID = "channel_bar";
     NotificationChannel channel = new NotificationChannel(CHANNEL_ID,
                                               context.getResources().getString(R.string.content_txt_43),
                                               NotificationManager.IMPORTANCE_NONE);

     ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
 
     Notification notification = new androidx.core.app.NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentTitle("")
        .setAutoCancel(false)
        .setShowWhen(false)
        .setSmallIcon(getPngImage(level))
        .setContentIntent(pendingIntent)
        .setContent(remoteViews)
        .build();

     notification.flags |= Notification.FLAG_NO_CLEAR;

     startForeground(NOTIFICATION_ID, notification);

    }

  }

}


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
}


* 메인 액티비티 클래스에서 시작하는 코드
Intent intent = new Intent(MainActivity.this, ManageService.class);

boolean isRunning = isServiceRunningCheck();
if (!isRunning) {
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     startForegroundService(intent);
   } else {
     startService(intent);
  }
}

테스트 폰이 4개 정도(오레오 버전 이상) 있는데.. 모두 정상 동작하며 오류가 발생하지않아요.
혹시 해결법을 아시는 분 계신가요?
구글링하여도 딱히 해결법이 나오지 않네요.
제가 유추하는 바로는 휴대폰 자체적으로 백그라운드서비스 제한에 걸려서 시작를 못해서 발생되는 오류가 아닐까 라는 의심만 하고 있네요.

나올레옹 (120 포인트) 님이 2020년 3월 23일 질문

1개의 답변

0 추천
startForeground는 Service의 onCreate에 들어가기 보다는 onStartCommand에 들어가야 문제가 생길 가능성이 적습니다. onStartCommand로 옮겨보세요.
회색 (21,000 포인트) 님이 2020년 3월 23일 답변
안녕하세요. 댓글 감사드립니다.
onCreate() 와 onStartCommand() 둘 모두 이미 오래전에 테스트를  하였습니다.
여전히 비슷한 수치로 같은 오류가 보고 됩니다.
...