Android

μ•ˆλ“œλ‘œμ΄λ“œ μŠ€νŠœλ””μ˜€) 앱이 ν™œμ„± μƒνƒœμΌ λ•Œ 계속 μ‹€ν–‰λ˜λŠ” ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€

567Rabbit 2024. 7. 2. 17:48

 

ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€λž€?

 

- ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€λŠ” μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ—μ„œ μ‚¬μš©μž κ²½ν—˜μ„ κ°œμ„ ν•˜κ³ , μ€‘μš”ν•œ μž‘μ—…μ„ μ•ˆμ •μ μœΌλ‘œ μ²˜λ¦¬ν•  수 μžˆλŠ” μ€‘μš”ν•œ 도ꡬ

- ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€λŠ” μ‚¬μš©μžκ°€ 앱을 μ‚¬μš©ν•˜κ³  μžˆμ„ λ•Œλ„ μ•Œλ¦Όμ„ 톡해 ν™œμ„± μƒνƒœμž„μ„ 보여주기 λ•Œλ¬Έμ—, μ‚¬μš©μžλŠ” μ–Έμ œλ“ μ§€ 이 μ„œλΉ„μŠ€λ₯Ό μ€‘μ§€ν•˜κ±°λ‚˜ μƒνƒœλ₯Ό 확인할 수 μžˆλ‹€.

- μ΄λŸ¬ν•œ νŠΉμ„± λ•Œλ¬Έμ— μ•ˆλ“œλ‘œμ΄λ“œ μ‹œμŠ€ν…œμ€ ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€λ₯Ό λ‹€λ₯Έ λ°±κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€λ³΄λ‹€ μš°μ„ μ μœΌλ‘œ 닀루며, ν•„μš”ν•œ λ¦¬μ†ŒμŠ€μ™€ κΆŒν•œμ„ μ œκ³΅ν•œλ‹€.

 

 

ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€ μ‚¬μš© μ˜ˆμ‹œ

 

μŒμ•… μž¬μƒ: μŒμ•… μ•±μ—μ„œλŠ” ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€λ₯Ό μ‚¬μš©ν•˜μ—¬ μ‚¬μš©μžκ°€ 앱을 μ’…λ£Œν•˜λ”λΌλ„ μŒμ•…μ„ 계속 μž¬μƒν•  수 μžˆλ‹€. 이 μ„œλΉ„μŠ€λŠ” μ•Œλ¦Όμ„ 톡해 μ‚¬μš©μžμ—κ²Œ ν˜„μž¬ μž¬μƒ 쀑인 μŒμ•…μ„ 보여주며, μŒμ•… μ œμ–΄ κΈ°λŠ₯도 μ œκ³΅ν•  수 μžˆλ‹€.

 

μœ„μΉ˜ 좔적: μœ„μΉ˜ 기반 μ•±μ—μ„œλŠ” ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€λ₯Ό μ‚¬μš©ν•˜μ—¬ μ‚¬μš©μžμ˜ μœ„μΉ˜λ₯Ό μ§€μ†μ μœΌλ‘œ 좔적할 수 μžˆλ‹€. μ΄λŠ” μ‚¬μš©μžμ—κ²Œ μ€‘μš”ν•œ κΈ°λŠ₯이며, 앱이 λ°±κ·ΈλΌμš΄λ“œμ—μ„œλ„ μ •ν™•ν•œ μœ„μΉ˜ μ—…λ°μ΄νŠΈλ₯Ό μ œκ³΅ν•  수 μžˆλ‹€. 예) λ‚΄λΉ„κ²Œμ΄μ…˜ μ•±, μš΄λ™ 좔적 μ•±

 

λ„€νŠΈμ›Œν¬ μš”μ²­ 처리: λ„€νŠΈμ›Œν¬ μš”μ²­μ΄ κΈ΄ μ‹œκ°„ λ™μ•ˆ ν•„μš”ν•œ 경우, ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€λ₯Ό μ‚¬μš©ν•˜μ—¬ μ•ˆμ •μ μœΌλ‘œ μ²˜λ¦¬ν•  수 μžˆλ‹€. 예) 파일 λ‹€μš΄λ‘œλ“œ, 데이터 동기화, λ‰΄μŠ€ μ•Œλ¦Ό λ“±

 

 

 

 

ν™˜κ²½μ„€μ •ν•˜κΈ°

 

 

 

 

 

 

 

TrackingService.java

 

package com.~.tmap.services;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import com.~.tmap.R;


public class TrackingService extends Service {
    private static final int NOTIFICATION_ID = 1;
    private static final String CHANNEL_ID = "my_foreground_service_channel";

    @Override
    public void onCreate() {
        // ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€κ°€ 생성될 λ•Œ μ‹€ν–‰λ˜λŠ” μ΄ˆκΈ°ν™” μ½”λ“œ
        super.onCreate();
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        // ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€λ₯Ό μœ„ν•œ μ•Œλ¦Ό 생성
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€ μ‹€ν–‰ 쀑")
                .setContentText("μ„œλΉ„μŠ€κ°€ μ‹€ν–‰ μ€‘μž…λ‹ˆλ‹€.")
                .setSmallIcon(R.drawable.ic_notification_icon)
                .build();

        // ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€ μ‹œμž‘
        startForeground(NOTIFICATION_ID, notification);
        // ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€μ—μ„œ μˆ˜ν–‰ν•  μž‘μ—…μ„ μ—¬κΈ° μ•„λž˜λΆ€ν„° κ΅¬ν˜„ν•˜λ©΄ 됨

        
        return START_STICKY;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopForeground(true); // ν¬κ·ΈλΌμš΄λ“œ μ„œλΉ„μŠ€ μ’…λ£Œ
    }
}