Created
March 5, 2020 11:43
-
-
Save Gongcu/e61dfec29d7dd668c4125ade25568a58 to your computer and use it in GitHub Desktop.
Background Service(JobIntentService) + BroadcastReceiver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.health.myapplication.alarm; | |
| import android.app.NotificationChannel; | |
| import android.app.NotificationManager; | |
| import android.app.PendingIntent; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.graphics.Color; | |
| import android.os.Build; | |
| import androidx.annotation.NonNull; | |
| import androidx.core.app.JobIntentService; | |
| import androidx.core.app.NotificationCompat; | |
| import androidx.core.app.TaskStackBuilder; | |
| import com.health.myapplication.R; | |
| import com.health.myapplication.activity.MainActivity; | |
| import java.text.SimpleDateFormat; | |
| import java.util.Calendar; | |
| import java.util.Date; | |
| import java.util.Locale; | |
| public class AlarmService extends JobIntentService { | |
| static final int JOB_ID = 1001; | |
| private Calendar calendar; | |
| //receiver에서 실행하는 함수 | |
| static void enqueueWork(Context context, Intent work) { | |
| enqueueWork(context, AlarmService.class, JOB_ID, work); | |
| } | |
| @Override | |
| protected void onHandleWork(@NonNull Intent intent) { | |
| calendar=Calendar.getInstance(); | |
| notification("HEALTH-ER", "message", AlarmService.this); //notification 함수 | |
| stopSelf(); | |
| } | |
| public void notification(String title, String message, Context context) { | |
| String text = "더 쉬면 근손실."; | |
| NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); | |
| int notificationId = createID(); | |
| String channelId = "channel-id"; | |
| String channelName = "Channel Name"; | |
| int importance = NotificationManager.IMPORTANCE_HIGH; | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | |
| NotificationChannel mChannel = new NotificationChannel( | |
| channelId, channelName, importance); | |
| notificationManager.createNotificationChannel(mChannel); | |
| } | |
| NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId) | |
| .setContentTitle(title) | |
| .setContentText(text) | |
| .setVibrate(new long[]{100, 250}) | |
| .setLights(Color.YELLOW, 500, 5000) | |
| .setAutoCancel(true); | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
| mBuilder.setSmallIcon(R.drawable.noti); | |
| mBuilder.setColor(getResources().getColor(R.color.colorPrimaryDark)); | |
| } else { | |
| mBuilder.setSmallIcon(R.drawable.noti); | |
| } | |
| TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); | |
| stackBuilder.addNextIntent(new Intent(context, MainActivity.class)); | |
| PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT); | |
| mBuilder.setContentIntent(resultPendingIntent); | |
| notificationManager.notify(notificationId, mBuilder.build()); | |
| } | |
| public int createID() { | |
| Date now = new Date(); | |
| int id = Integer.parseInt(new SimpleDateFormat("ddHHmmss", Locale.KOREA).format(now)); | |
| return id; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| startButton.setOnClickListener{ | |
| // Creating the pending intent to send to the BroadcastReceiver | |
| try { | |
| alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager | |
| val intent = Intent(this, MyAlarmReceiver::class.java) | |
| pendingIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT) | |
| // Setting the specific time for the alarm manager to trigger the intent | |
| val calendar = Calendar.getInstance() | |
| calendar.timeInMillis = System.currentTimeMillis() | |
| //0~60초, 분 이내의 시간 입력 | |
| if(Integer.parseInt(secondEditText.text.toString())>60 || Integer.parseInt(secondEditText.text.toString())<0 || | |
| Integer.parseInt(minuteEditText.text.toString())>60 || Integer.parseInt(minuteEditText.text.toString())<0 ){ | |
| Toast.makeText(this@MainActivity, "올바른 시간을 입력하세요.", Toast.LENGTH_LONG).show() | |
| } | |
| else { | |
| calendar.add(Calendar.SECOND, Integer.parseInt(secondEditText.text.toString())) | |
| calendar.add(Calendar.MINUTE, Integer.parseInt(minuteEditText.text.toString())) | |
| if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { | |
| alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent) | |
| } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { | |
| alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent) | |
| } else { | |
| alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent) | |
| } | |
| Toast.makeText(this@MainActivity, minuteEditText.text.toString() + "분 " + secondEditText.text.toString() + "초 뒤에 알람이 울립니다", Toast.LENGTH_SHORT).show() | |
| } | |
| }catch (e: Exception) { | |
| Toast.makeText(this@MainActivity, "알람 설정 오류", Toast.LENGTH_SHORT).show() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| package="com.health.myapplication"> | |
| <!-- 진동 허용, 부팅 시 receive 허용, 화면 out 상태에서 켜짐 허용 --> | |
| <uses-permission android:name="android.permission.WAKE_LOCK" /> | |
| <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> | |
| <uses-permission android:name="android.permission.VIBRATE" /> | |
| <application | |
| ... | |
| <receiver android:name=".alarm.MyAlarmReceiver"/> | |
| <service | |
| android:name=".alarm.AlarmService" | |
| android:enabled="true" | |
| android:exported="false" | |
| android:permission="android.permission.BIND_JOB_SERVICE" /> | |
| </application> | |
| </manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.health.myapplication.alarm; | |
| import android.content.BroadcastReceiver; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| public class MyAlarmReceiver extends BroadcastReceiver { | |
| private static Context context; | |
| @Override | |
| public void onReceive(Context context, Intent intent) { | |
| //Receiver가 알람을 받음 | |
| this.context = context; | |
| Intent service_intent = new Intent(context, AlarmService.class); | |
| AlarmService.enqueueWork(context,service_intent); //service 시작 | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment