기본 콘텐츠로 건너뛰기

안드로이드 앱 만들기 : Alarm manager vs Job Scheduler vs Worker


원본출처: 티스토리 바로가기

 

손에 들고 다니는 스마트폰에 무슨 일을 그렇게 시켜 먹을라고(?) 이런 것들이 있는 가? 하는 생각이 들 무렵입니다. 

그래도 우린 이제 이런 배치(반복작업을 위한) 처리를 해야 하는 경우가 있어서 이런 것들에 대해서 알아 보고자 합니다. 

 

  • Alarm manager
  • Job Scheduler
  • Worker

반복적인 일을 시키는 방법 3가지를 살펴 보고자 합니다.

Alarm manager

알림은 지정한 시간에 어떤 이벤트가 있는 지를 알려주는 역할을 하게 됩니다.  그것들 중에서 1회성 알림만을 사용하는 경우도 있기는 하겠지만,  앱을 개발하는 경우에는 반복적인 알림을 발생시키는 것이 좋을 때가 있습니다.  그때 사용을 하게 될 것 같습니다. 

 

알림 매니저가 좋은 건 15분 미만의 경우도 반복 작업을 할 수 있다는 점 입니다.

https://developer.android.com/reference/android/app/AlarmManager

 

AlarmManager  |  Android Developers

 

developer.android.com

 

Job Scheduler

 

이름 그대로 일을 스케줄에 맞게 반복적인 작업을 실행하는 경우에 사용하게 될 것 같습니다. 다만,  안드로이드 버전이 높아지면서 배터리 효율을 관리하기 위해 최소 시간이 15분이라는 간격을 유지해야 한다는 불편함(?)이 있다는 것이 아쉽게 다가올 뿐입니다. 

 

https://developer.android.com/reference/android/app/job/JobScheduler

 

JobScheduler  |  Android Developers

 

developer.android.com

 

Worker

비동기식 작업을 설정하는 방법 중에 하나라고는 들었으나, 아직 아는 바가 없어 이 부분에 대한 기술은 훗날로 미루어 봅니다. ㅋ~

https://developer.android.com/reference/androidx/work/Worker

 

Worker  |  Android Developers

androidx.constraintlayout.core.motion.parse

developer.android.com

 

구현해 보기

이제 코드로 하나씩 만들어 보겠습니다.  

 

JobScheduler 관리를 위해서 다음과 같이 선언해 둡니다. jobId 가 있어야 하기 때문이기도 하고, 반복 간격을 지정하기 위한 상수도 선언합니다. 그리고 성공 여부를 확인 하기 위한 상수도 같이 선언 합니다.

private val JOB_ID = 123 private var PERIODIC_TIME: Long = 15 * 60 * 1000 private val SUCCESS_KEY = "SUCCESS" private val FAILED_KEY = "FAILED"

 

알림 설정을 위해서는 다음과 같이 알림 매니저와 알림 실행 시 사용한 intent 변수 하나를 설정해 둡니다.

private var alarmMgr: AlarmManager? = null private lateinit var alarmIntent: PendingIntent

 

JobScheduler의 시작은 다음과 같이 코드 작업을 합니다.  jobServices는 반복 작업에서 실행시킬 class 이름입니다. 

  • setPersisted : 부팅된 이후에도 반복을 하게 할지 여부를 선택합니다.
  • setPeriodic : 반복 시간을 mS 단위로 설정하게 됩니다.

다른 선택 값들도 있으니 참고해서 살펴보세요.

val componentName = ComponentName(this, jobServices::class.java) val info = JobInfo.Builder(JOB_ID, componentName)     .setPersisted(true)             // true 부팅된 이후에도 반복하게     .setPeriodic(PERIODIC_TIME) // 지연시간     .build()  val jobScheduler: JobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler val resultCode = jobScheduler.schedule(info)  val isJobScheduledSuccess = resultCode == JobScheduler.RESULT_SUCCESS Log.e(TAG, "Job Scheduled ${if (isJobScheduledSuccess) SUCCESS_KEY else FAILED_KEY}")

 

알림의 경우도 알림 상수로 선언한 알림 매니저에 알림 실행 시 사용할 AlarmReceiver을 선언하고  알림 설정을 시작합니다.  반복 작업을 할 예정이기 때문에 Repeting 함수들 중에서 골랐습니다.  반복이 아닌 1회성의 경우는 다른 함수를 사용하게 됩니다. 

  • setInexactRepeating : 알림 시간 이후 반복 시간 동안 절전모드를 해제하여 알림을 발생시킵니다.
  • set : 시정된 시간에 알림을 1회성으로 절전모드를 해제하여 알림을 발생시킵니다.
alarmMgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager alarmIntent = Intent(applicationContext, AlarmReceiver::class.java).let { intent ->     PendingIntent.getBroadcast(applicationContext, 0, intent, FLAG_MUTABLE) }  alarmMgr?.setInexactRepeating(     AlarmManager.ELAPSED_REALTIME_WAKEUP,     SystemClock.elapsedRealtime() + sp.getFloat("repeatTerm", 1f).toLong() * 60 * 1000, // 1분 단위로     sp.getFloat("repeatTerm", 1f).toLong() * 60 * 1000,     alarmIntent )

 

이제 코드 작업을 시작하였으니 작업 해제 하는 코드를 살펴보겠습니다. 

 

JobScheduler의 경우는 앞에서 시작할 때 지정했던 jobID을 이용하여 반복 작업을 하는 하는 반면에...

val jobScheduler: JobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler jobScheduler.cancel(JOB_ID) Log.e(TAG, "Job CANCELED")

 

알림의 경우는 알림 매니저를 그냥 cancel 하는 것으로 작업이 종료됩니다.

alarmMgr?.cancel(alarmIntent)

 

이제 각 배치 작업에서 사용할 Service의 코드를 보겠습니다. 

먼저 manifest.xml 파일에 등록한 부분을 보겠습니다. 

<receiver android:name=".receivers.AlarmReceiver"     android:exported="false"/>  <service     android:name=".services.jobServices"     android:exported="true"     android:permission="android.permission.BIND_JOB_SERVICE" />

알림을 위해서는 receiver로 등록을 하게 되고, jobscheduler을 위해서는 jobservices로 등록을 하게 됩니다.  각 class의 전체 코드는 다음과 같습니다. 

 

JobServices 

import android.annotation.SuppressLint import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.app.job.JobParameters import android.app.job.JobService import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.graphics.Color import android.os.Build import android.os.Bundle import android.util.Log import androidx.annotation.RequiresApi import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import com.billcoreatech.smsreceiver1113.BuildConfig import com.billcoreatech.smsreceiver1113.MainActivity import com.billcoreatech.smsreceiver1113.R import com.billcoreatech.smsreceiver1113.retrofit.RetrofitService import java.text.SimpleDateFormat import java.util.*  @SuppressLint("SpecifyJobSchedulerIdRange") class jobServices : JobService() {      companion object {         private val TAG = "MyJobService"         lateinit var sp: SharedPreferences     }      @RequiresApi(Build.VERSION_CODES.S)     override     fun onStartJob(params: JobParameters): Boolean {         var strDate = System.currentTimeMillis()         var sdf = SimpleDateFormat("yyyy-MM-dd kk:mm:ss", Locale("ko", "KR"))         var now = sdf.format(strDate )         var context = this@jobServices          sp = getSharedPreferences(packageName, MODE_PRIVATE)          Log.e(TAG, "${now} onStartJob: ${params.jobId} ${sp.getFloat("repeatTerm", 1f).toLong()} ${sp.getBoolean("jobScheduled", false)}")         sendNotification(this@jobServices, "Activated ...")         return false     }      override     fun onStopJob(params: JobParameters): Boolean {         Log.e(TAG, "onStopJob: ${params.jobId}")         return false     }      @RequiresApi(Build.VERSION_CODES.S)     @SuppressLint("MissingPermission")     private fun sendNotification(context : Context, messageBody: String) {         val intent = Intent(context, MainActivity::class.java)         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)         val extras = Bundle()         extras.putString("MSGRCV", messageBody)         intent.putExtras(extras)         val pendingIntent = PendingIntent.getActivity(             context, 0 /* Request code */, intent,             PendingIntent.FLAG_MUTABLE         )          val channelId: String = context.getString(R.string.default_notification_channel_id)         val channelName: CharSequence = context.getString(R.string.default_notification_channel_name)         val importance = NotificationManager.IMPORTANCE_HIGH         val notificationChannel = NotificationChannel(channelId, channelName, importance)         notificationChannel.enableLights(true)         notificationChannel.lightColor = Color.RED         notificationChannel.enableVibration(true)         notificationChannel.vibrationPattern =             longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400)          val wearNotifyManager = NotificationManagerCompat.from(context)         val wearNotifyBuilder: NotificationCompat.Builder =             NotificationCompat.Builder(context, channelId)                 .setSmallIcon(R.drawable.ic_outline_sms_24)                 .setContentTitle(context.getString(R.string.app_name))                 .setContentText(messageBody)                 .setAutoCancel(true)                 .setContentIntent(pendingIntent)                 .setVibrate(longArrayOf(100, 200, 300, 400, 500, 400, 300, 200, 400))                 .setDefaults(-1)          wearNotifyManager.createNotificationChannel(notificationChannel)         wearNotifyManager.notify(0, wearNotifyBuilder.build())     } }

 

AlarmReceiver

 import android.app.AlarmManager import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.Build import android.os.SystemClock import android.util.Log import androidx.activity.ComponentActivity import androidx.annotation.RequiresApi import androidx.core.app.NotificationCompat import com.billcoreatech.smsreceiver1113.MainActivity import com.billcoreatech.smsreceiver1113.R  class AlarmReceiver : BroadcastReceiver() {      companion object {         const val TAG = "AlarmReceiver"         const val NOTIFICATION_ID = 0         const val PRIMARY_CHANNEL_ID = "primary_notification_channel"     }      lateinit var notificationManager: NotificationManager      @RequiresApi(Build.VERSION_CODES.S)     override fun onReceive(context: Context, intent: Intent) {         Log.d(TAG, "Received intent : $intent")         notificationManager = context.getSystemService(             Context.NOTIFICATION_SERVICE) as NotificationManager          createNotificationChannel(context)         deliverNotification(context)      }      @RequiresApi(Build.VERSION_CODES.S)     private fun deliverNotification(context: Context) {         val contentIntent = Intent(context, MainActivity::class.java)         val contentPendingIntent = PendingIntent.getActivity(             context,             NOTIFICATION_ID,             contentIntent,             PendingIntent.FLAG_MUTABLE         )         var sp = context.getSharedPreferences(context.packageName, Context.MODE_PRIVATE)          val builder =             NotificationCompat.Builder(context, PRIMARY_CHANNEL_ID)                 .setSmallIcon(R.drawable.ic_outline_sms_24)                 .setContentTitle(context.getString(R.string.app_name))                 .setContentText("This is repeating alarm repeatTerm ${sp.getFloat("repeatTerm", 1f).toLong()} min")                 .setContentIntent(contentPendingIntent)                 .setPriority(NotificationCompat.PRIORITY_HIGH)                 .setAutoCancel(true)                 .setDefaults(NotificationCompat.DEFAULT_ALL)          Log.e("", "This is repeating alarm repeatTerm ${sp.getFloat("repeatTerm", 1f).toLong()} min")          notificationManager.notify(NOTIFICATION_ID, builder.build())     }      private fun createNotificationChannel(context: Context) {          val notificationChannel = NotificationChannel(             PRIMARY_CHANNEL_ID,             context.getString(R.string.app_name),             NotificationManager.IMPORTANCE_HIGH         )         notificationChannel.enableLights(true)         notificationChannel.lightColor = R.color.softRed         notificationChannel.enableVibration(true)         notificationChannel.description = context.getString(R.string.app_name)         notificationManager.createNotificationChannel(             notificationChannel)      } }

 

두 가지 케이스 모두 그냥 job에서는 Notification을 발생시키는 것이 목적인 코드입니다.  이제 코드작성 방법은 다 모았으니 다른 작업으로 발전해 나갈 수 있겠지요?

 

 

귤탐 당도선별 감귤 로열과, 3kg(S~M), 1박스 삼립 호빵 발효미종 단팥, 92g, 14개입 [엉클컴퍼니] 우리밀 찐빵/흑미찐빵/단호박찐빵/고구마찐빵 국산팥, 우리밀 고구마찐빵(20개입) 1300g 국산팥 우리밀 MORIT 여성용 방한장갑 터치스크린 다용도 고급겨울장갑 에이치머스 스마트폰 터치 방한 장갑
이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다.

댓글

이 블로그의 인기 게시물

개인정보처리방침 안내

 billcoreaTech('https://billcoreatech.blogspot.com/'이하 'https://billcoreatech.blogspot')은(는) 「개인정보 보호법」 제30조에 따라 정보주체의 개인정보를 보호하고 이와 관련한 고충을 신속하고 원활하게 처리할 수 있도록 하기 위하여 다음과 같이 개인정보 처리방침을 수립·공개합니다. ○ 이 개인정보처리방침은 2021년 8월 26부터 적용됩니다. 제1조(개인정보의 처리 목적) billcoreaTech('https://billcoreatech.blogspot.com/'이하 'https://billcoreatech.blogspot')은(는) 다음의 목적을 위하여 개인정보를 처리합니다. 처리하고 있는 개인정보는 다음의 목적 이외의 용도로는 이용되지 않으며 이용 목적이 변경되는 경우에는 「개인정보 보호법」 제18조에 따라 별도의 동의를 받는 등 필요한 조치를 이행할 예정입니다. 1. 서비스 제공 맞춤서비스 제공을 목적으로 개인정보를 처리합니다. 제2조(개인정보의 처리 및 보유 기간) ① billcoreaTech은(는) 법령에 따른 개인정보 보유·이용기간 또는 정보주체로부터 개인정보를 수집 시에 동의받은 개인정보 보유·이용기간 내에서 개인정보를 처리·보유합니다. ② 각각의 개인정보 처리 및 보유 기간은 다음과 같습니다. 1.<서비스 제공> <서비스 제공>와 관련한 개인정보는 수집.이용에 관한 동의일로부터<사용자의 설정시간>까지 위 이용목적을 위하여 보유.이용됩니다. 보유근거 : 앱의 기본기능 활용에 필요한 위치정보 제3조(개인정보의 제3자 제공) ① billcoreaTech은(는) 개인정보를 제1조(개인정보의 처리 목적)에서 명시한 범위 내에서만 처리하며, 정보주체의 동의, 법률의 특별한 규정 등 「개인정보 보호법」 제17조 및 제18조에 해당하는 경우에만 개인정보를 제3자에게 제공합니다. ② billcoreaTech

안드로이드 앱 만들기 : jetpack compose URL 에서 image 받아와서 보여 주기 (feat coil)

원본출처: 티스토리 바로가기 샘플 이미지 오늘은 앱을 만들면서 이미지를 보여 주어야 하는 경우 중에  URL에서 이미지를 가져와 보는 것에 대해서 기술해 보겠습니다.  URL에서 image를 가져온다는 것은 서버에 저장된 image 일수도 있고, SNS profile의 image 정보일수도 있을 것입니다.    구글에서 찾아보면 다른 것들도 많이 있기는 합니다.  그 중에서 coil 라이브러리를 이용해서 한번 만들어 보도록 하겠습니다.    gradle 설정 그래들 설정은 아래 한 줄입니다. 현재 시점에서 최신 버전인 것으로 보입니다.  이 글을 보시는 시점에 최신 버전이 아니라면 아마도 android studio는 추천을 해 줍니다. // image load from url implementation("io.coil-kt:coil-compose:2.2.2")   manaifest  설정 당연한 이야기 겠지만, url에서 이미지를 받아 와야 하기 때문에 권한 설정을 해야 합니다. 또한 인터넷에서 자료를 받아 오는 것은 지연이 발생할 수 있기 때문에 application에서도 다음 문구의 추가가 필요합니다.  <uses-permission android:name="android.permission.INTERNET" /> <application ... android:networkSecurityConfig="@xml/network_config" ... /> 이 설정을 하게 되면 xml 파일을 하나 추가해 주면 됩니다.  network_config.xml 이라는 이름으로 말입니다.  <?xml version="1.0" encoding="utf-8"?> <network-security-config> <domain-config cleartextT