๋ชฉ์ฐจ
๐ ๋ณธ๊ฒฉ์ ์ผ๋ก ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
์ด์ ์ ๋ฆฌํ ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์๋น์ค๋ฅผ ์ด์ฉํด์ ๊ตฌํํ ์์น , ๋น์ฝ ๋ฐ์ดํฐ๋ฅผ ์ธก์ ํ๋ ๊ธฐ๋ฅ์ ๊ตฌํํด๋ณด์.
๊ตฌํ์ ์์ ์๊ตฌ์ฌํญ์ ์ ๋ฆฌํ๊ณ ๊ตฌํํ ๊ธฐ๋ฅ์ ํ์ํ ๊ธฐ์ ์ ๊ฒฐ์ ํด๋ณด์
๊ธฐ๋ฅ ์๊ตฌ ์ฌํญ
1. ์ฑ์ด ํ๋ฉด์ ๋ณด์ด์ง ์์๋ ๋ฐ์ดํฐ ์ธก์ ์ด ๋์ด์ผํ๋ค.
2. ์ฌ์ฉ์๊ฐ ์ค์ ํ ์ฃผ๊ธฐ๋ง๋ค ๋ฐ์ดํฐ๋ฅผ ์ธก์ ํด์ผํ๋ค.
3. ์ฌ์ฉ์๊ฐ ์ค์ ํ ์ฃผ๊ธฐ(5์ด ~ 1๋ถ)๋ง๋ค ์ธก์ ๋ ๋ฐ์ดํฐ๋ฅผ ์๋ฒ๋ก ์ ์กํด์ผํ๋ค.
4.. ์ฌ์ฉ์๊ฐ ์ค์ ํ ์๊ฐ๋์(ex 09:00 ~ 18:00) ๋์ ์ง์์ ์ผ๋ก ์์ ์ด ์ด๋ฃจ์ด์ ธ์ผํ๋ค.
๊ธฐ์ ์ ํ ๊ณผ์
- WorkManager๋ฅผ ์ฌ์ฉํ์ง ์์ ์ด์
๋ฐฑ๊ทธ๋ผ์ด๋ ์์ ์ ๋ํ ๊ฒ์์ ํ์ ๋ ๊ฐ์ฅ ๋จผ์ ๋ฑ์ฅํ ๊ธฐ์ ์ WorkManager์๋ค. Workmanager๋ Jetpack์ ๋ฐฑ๊ทธ๋ผ์ด๋ ์ฒ๋ฆฌ๋ฅผ ๋์์ฃผ๊ธฐ ์ํด ๋ง๋ ์์๋ก ๊ฐ์ฅ ์ต์ ์ ๋์จ ๊ธฐ์ ์ด์๋ค. ๊ทธ๋ฌ๋ ์๋์ ์ด์ ๋ค๋ก WorkManager ๋ฐฉ์์ ์ฐ๋ฆฌ ํ๋ก์ ํธ์ ์ ํฉํ์ง ์๋ค๊ณ ํ๋จํ์๋ค.
1. ์ ๊ณตํ๋ ์ต์ ๊ฐ๊ฒฉ์ด 15๋ถ์ผ๋ก ์ด ๋จ์์ ๊ฐ๊ฒฉ์ ๊ตฌํํด์ผํ๋ ์ฐ๋ฆฌ์ ์๊ตฌ ์ฌํญ์ ์ ํฉํ์ง ์์๋ค.
2. ์ธก์ ํ ๋ฐ์ดํฐ๋ฅผ ์ค์๊ฐ์ผ๋ก ํ๋ฉด์ ์ ๋ฐ์ดํธํด์ผํ๊ธฐ ๋๋ฌธ์ WorkManager๋ ์ ํฉํ์ง ์์๋ค.
์ ์ด์ ๋ก WorkManager ๋ฐฉ์์ ์ฐ๋ฆฌ ํ๋ก์ ํธ์ ์ ํฉํ์ง ์์๋ค.
๋ฐ๋ผ์ ForegroundService ๋ฐฉ์์ ์ฌ์ฉํ์ฌ ๊ธฐ๋ฅ์ ๊ตฌํํ๊ฒ ๋์๋ค.
๊ตฌํ ํ๋ฆ
ForegroundService ์ฌ์ฉํ๊ธฐ
// ํฌ๊ทธ๋ผ์ด๋ ์์ฑ ์ฝ๋
class LocationForegroundService : Service() {
override fun onBind(intent: Intent?): IBinder? {
return null
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
createNotificationChannel()
startForeground(1,getNotification("๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ์ค"))
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
getLocation()
return START_STICKY
}
1. ์๋ฆผ ์์ฒญ
ํฌ๊ทธ๋ผ์ด๋ ์๋น์ค๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ์๋ฆผ์ ๊ตฌํํด์ค์ผํ๋ค.
// ์๋ฆผ ์ฑ๋ ์์ฑ
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val serviceChannel = NotificationChannel(
CHANNEL_ID, "location service channel", NotificationManager.IMPORTANCE_DEFAULT)
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(serviceChannel)
}
}
// ์๋ฆผ๋ด์ฉ ์
๋ฐ์ดํธ
private fun updateNotification(latitude : Double, longitude: Double,time:Long){
val notification = getNotification("์๋ : $latitude, ๊ฒฝ๋: $longitude, ์๊ฐ : $time")
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1,notification)
}
// ์์น ์๋ฆผ ๊ตฌ์ฑ
private fun getNotification(contentText : String) : Notification {
val notificationIntent = Intent(this,LocationActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,0,notificationIntent,PendingIntent.FLAG_IMMUTABLE
)
val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("location service")
.setContentText(contentText)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setCategory(NotificationCompat.CATEGORY_LOCATION_SHARING)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setVibrate(longArrayOf(0))
builder.setForegroundServiceBehavior(Notification.FOREGROUND_SERVICE_IMMEDIATE)
return builder.build()
}
2. ์์น ์์ฒญ
// ์์น ๋ฐ์ดํฐ ์ธก์
private fun getLocation() {
val mLocationRequestHighAccuracy = LocationRequest.create().apply {
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
interval = 5 * 1000 // 1-minute interval
fastestInterval = 5 * 1000
}
// ๊ถํ ๊ฒ์ฌ
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient.requestLocationUpdates(
mLocationRequestHighAccuracy,
object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult) {
for (location in locationResult.locations) {
location?.let {
// ์๋ฆผ์
๋ฐ์ดํธ
updateNotification(it.latitude, it.longitude, it.time)
}
'''
3. ํฌ๊ทธ๋ผ์ด๋ ์๋น์ค ์คํ
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
// 1. ์๋ฆผ
createNotificationChannel()
startForeground(1,getNotification("๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ์ค"))
// 2. ์์น ์์ฒญ
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
getLocation()
return START_STICKY
}
๊ทธ๋ฆฌ๊ณ ๊ทธ ๊ฒฐ๊ณผ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์๋์ ๊ฐ์ด ์์น ๋ฐ์ดํฐ๋ฅผ ์ธก์ ํ๋๋ฐ ์ฑ๊ณตํ๋ค!
๐ก๋ฐฐ์ด ์
- ๊ถํ ์์ฒญ ๋ฐ ์ฌ์ฉ์์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ์ ๋ฐ๋ฅธ ๋์
- ๋ฐฑ๊ทธ๋ผ์ด๋(ํฌ๊ทธ๋ผ์ด๋ ์๋น์ค)๋ฅผ ํตํด ์์น ๊ฐ์ ๋ฐ์์ค๋ ๋ฐฉ๋ฒ
- ๋ฐฑ๊ทธ๋ผ์ด๋(ํฌ๊ทธ๋ผ์ด๋ ์๋น์ค) ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ
- ์๋ฆผ(notification)์ ๋์ฐ๋ ๋ฐฉ๋ฒ
- ์๋ฆผ์ ๋์ฐ๊ธฐ ์ํด์ manifest ๋ฟ ์๋๋ผ ๋ฐํ์ ๊ถํ์ ์์ฒญํด์ผํ๋ค.
- ๊ถํ์ ๋ํ ์ค๋ช ๊ณผ ๊ณผ์ ์ ์๋ ๊ธ์ ์ ๋ฆฌํด๋์๋ค.
Android ๊ถํ ์์ฒญ ์ฒ๋ฆฌํ๊ธฐ (๋ฐํ์ ๊ถํ??)
๋ค์ด๊ฐ๊ธฐ ์ ์ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉฐ ์ฌ์ฉ์์ ์์น๋ฅผ ์ธก์ ํ๋ ๊ธฐ๋ฅ์์๋ ์์น ๊ถํ ์์ฒญ์ด๋ผ๋ ๊ถํ์ด ํ์ํ๋ค. ์ฒ์์๋ ๊ถํ ์์ฒญ์ ๋ํด ๋์๋กญ์ง ์๊ฒ ์ฌ๊ฒผ๋๋ฐ, ๊ถํ์ ์ ๋๋ก ์ค์ ํด
hyeonhahaha.tistory.com
'๐ค2024 ์๋๋ก์ด๋ > โ๏ธ ํ๊ณ ์ธํด ๊ฐ๋ฐ ํ๋ก์ ํธ ๊ธฐ๋ก' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Android : Handler ์ดํดํ๊ธฐ (0) | 2024.08.29 |
---|---|
Android ๊ถํ ์์ฒญ ์ฒ๋ฆฌํ๊ธฐ (๋ฐํ์ ๊ถํ??) (0) | 2024.08.23 |
Android Thread (0) | 2024.08.19 |
๐ ํ๋ก์ ํธ ์๋ฃ ํ 4L ํ๊ณ (0) | 2024.08.19 |
๐ Android ์ 4๋ ๊ตฌ์ฑ์์ Service์ ๋ํด ์์๋ณด์! (0) | 2024.08.12 |