투케이2K

986. (Android/Java) [간단 소스] 안드로이드 14 대응 관련 포그라운드 서비스 호출 방법 - startForeground Notification 본문

Android

986. (Android/Java) [간단 소스] 안드로이드 14 대응 관련 포그라운드 서비스 호출 방법 - startForeground Notification

투케이2K 2025. 5. 19. 20:01

[개발 환경 설정]

개발 툴 : AndroidStudio

개발 언어 : Java / Kotlin

 

[소스 코드]

// --------------------------------------------------------------------------------------
[개발 및 테스트 환경]
// --------------------------------------------------------------------------------------

- 언어 : Java / Kotlin

- 개발 툴 : AndroidStudio

- 기술 구분 : 포그라운드 서비스 / startForeground / Notification

// --------------------------------------------------------------------------------------






// --------------------------------------------------------------------------------------
[사전) 권한 설정 사항] : AndroidManifest.xml
// --------------------------------------------------------------------------------------

<!-- ======================== [위치 사용 권한] ======================== -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>


<!-- ======================= [위치 사용 서비스] ======================= -->
<service android:name=".util.LocationService"
  android:enabled="true"
  android:exported="false"
  android:foregroundServiceType="location"
  android:stopWithTask="false" />

// --------------------------------------------------------------------------------------





// --------------------------------------------------------------------------------------
[소스 코드]
// --------------------------------------------------------------------------------------


// --------------------------------------------------------
// [Android 26 오레오 이상 분기 처리 포그라운드 호출]
// --------------------------------------------------------
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
    createNotificationChannel();
} 
else {
  startForeground(1, new Notification());
}



// --------------------------------------------------------
// [포그라운드 서비스 호출 시작]
// --------------------------------------------------------
@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel(){

    Intent notificationIntent = new Intent(this, LoginActivity.class);
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent pendingIntent = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S){ // [API 레벨 31 이상 : 안드로이드 12]
        pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
    }else {
        pendingIntent = PendingIntent.getActivity(getBaseContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    }

    String notificationChannelId = "com.twok.javaproject";
    String channelName = "location Service";

    NotificationChannel channel = new NotificationChannel(notificationChannelId, channelName, NotificationManager.IMPORTANCE_HIGH); // [IMPORTANCE_HIGH 중요도 높음 설정]
    channel.setLightColor(Color.BLUE);
    channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel); // [채널 생성]

    Notification.Builder notificationBuilder = new Notification.Builder(this, notificationChannelId);
    notificationBuilder.setContentIntent(pendingIntent);
    Notification notification = notificationBuilder
            .setOngoing(true)
            .setSmallIcon(R.mipmap.noti)
            .setContentTitle(getString(R.string.app_title))
            .setContentText(getString(R.string.location_service))
            .setCategory(Notification.CATEGORY_SERVICE)
            .build();

    // [분기 처리 포그라운드 서비스 호출]
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        startForeground(1, notification, FOREGROUND_SERVICE_TYPE_LOCATION);
    }
    else{
        startForeground(1, notification);
    }

}

// --------------------------------------------------------------------------------------





// --------------------------------------------------------------------------------------
[참고 사이트]
// --------------------------------------------------------------------------------------

[안드로이드 디벨로퍼 : 포그라운드 서비스 유형은 필수 항목임]

https://developer.android.com/about/versions/14/changes/fgs-types-required?sjid=16763442678436777799-AP&hl=ko


[구글 콘솔 : 정책 공지]

https://support.google.com/googleplay/android-developer/answer/13411745?hl=ko

// --------------------------------------------------------------------------------------
 
반응형
Comments