반응형
2020/04/02 - [안드로이드/개발자 일상] - 안드로이드 AlarmManager 1-1 (매일 반복적으로 알람 받기)
AlarmManager 1-1에 추가로 코드를 작성하였습니다.
추가 사항
날짤 선택을 위한 CheckBox (월요일~일요일)
해당 시간에 액티비티를 실행
<실행 화면> 요일과 시간을 선택하여 알림을 받도록 했습니다.
activity_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TimePicker
android:id="@+id/tp_timepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:timePickerMode="spinner"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:onClick="regist"
android:text="등록"
app:layout_constraintBaseline_toBaselineOf="@+id/button2"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tp_timepicker" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:onClick="unregist"
android:text="해지"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/tp_timepicker" />
<CheckBox
android:id="@+id/cb_sun"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="일요일"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cb_mon"
android:layout_marginBottom="120dp"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
<CheckBox
android:id="@+id/cb_mon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="월요일"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cb_tue"
android:layout_marginBottom="120dp"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/cb_sun"
tools:ignore="MissingConstraints" />
<CheckBox
android:id="@+id/cb_tue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="화요일"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cb_wed"
app:layout_constraintHorizontal_bias="0.5"
android:layout_marginBottom="120dp"
app:layout_constraintStart_toEndOf="@+id/cb_mon"
tools:ignore="MissingConstraints" />
<CheckBox
android:id="@+id/cb_wed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="수요일"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="120dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/cb_tue"
tools:ignore="MissingConstraints" />
<CheckBox
android:id="@+id/cb_thu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="목요일"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cb_fri"
app:layout_constraintHorizontal_bias="0.5"
android:layout_marginBottom="50dp"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="MissingConstraints" />
<CheckBox
android:id="@+id/cb_fri"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="금요일"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/cb_sat"
app:layout_constraintHorizontal_bias="0.5"
android:layout_marginBottom="50dp"
app:layout_constraintStart_toEndOf="@+id/cb_thu"
tools:ignore="MissingConstraints" />
<CheckBox
android:id="@+id/cb_sat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="토요일"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
android:layout_marginBottom="50dp"
app:layout_constraintStart_toEndOf="@+id/cb_fri"
tools:ignore="MissingConstraints" />
|
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package com.example.ex_alarm;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TimePicker;
import android.widget.Toast;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private TimePicker timePicker;
private AlarmManager alarmManager;
private int hour, minute;
CheckBox cbSun, cbMon, cbTue, cbWed, cbThu, cbFri, cbSat;
static String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timePicker=findViewById(R.id.tp_timepicker);
alarmManager= (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
cbSun=findViewById(R.id.cb_sun);
cbMon=findViewById(R.id.cb_mon);
cbTue=findViewById(R.id.cb_thu);
cbWed=findViewById(R.id.cb_wed);
cbThu=findViewById(R.id.cb_thu);
cbFri=findViewById(R.id.cb_fri);
cbSat=findViewById(R.id.cb_sat);
}// onCreate()..
public void regist(View view) {
boolean[] week = { false, cbSun.isChecked(), cbMon.isChecked(), cbTue.isChecked(), cbWed.isChecked(),
cbThu.isChecked(), cbFri.isChecked(), cbSat.isChecked() }; // cbSun을 1번부터 사용하기 위해 배열 0번은 false로 고정
if(!cbSun.isChecked() && !cbMon.isChecked() && !cbTue.isChecked() && !cbWed.isChecked() && !cbThu.isChecked() && !cbFri.isChecked() && !cbSat.isChecked()){
Toast.makeText(this, "요일을 선택해 주세요.", Toast.LENGTH_SHORT).show();
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
hour=timePicker.getHour();
minute=timePicker.getMinute();
}else{
Toast.makeText(this, "버전을 확인해 주세요.", Toast.LENGTH_SHORT).show();
return;
}
Intent intent = new Intent(this, Alarm.class);
intent.putExtra("weekday", week);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0,intent, 0); //PendingIntent.FLAG_UPDATE_CURRENT
Calendar calendar = Calendar.getInstance();
Date today = new Date();
long intervalDay = 24 * 60 * 60 * 1000;// 24시간
long selectTime=calendar.getTimeInMillis();
long currenTime=System.currentTimeMillis();
//만약 설정한 시간이, 현재 시간보다 작다면 알람이 부정확하게 울리기 때문에 다음날 울리게 설정
if(currenTime>selectTime){
selectTime += intervalDay;
}
Log.e(TAG,"등록 버튼을 누른 시간 : "+today+" 설정한 시간 : "+calendar.getTime());
Log.d(TAG,"calendar.getTimeInMillis() : "+calendar.getTimeInMillis());
// 지정한 시간에 매일 알림
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, selectTime, intervalDay, pIntent);
}// regist()..
public void unregist(View view) {
Intent intent = new Intent(this, Alarm.class);
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
}// unregist()..
}// MainActivity class..
|
쉽게 확인하기 위해
해당 날짜 시간에 액티비티를 실행합니다.
새로운 액티비티를 만들어 줍니다.
activity_alarm_time_view.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AlarmTimeViewActivity">
<TextView
android:id="@+id/tv_alarm_set_day_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="테스트"
android:textSize="24sp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
|
AlarmTimeViewActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
package com.example.ex_alarm;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class AlarmTimeViewActivity extends AppCompatActivity {
TextView alarmSetTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_time_view);
alarmSetTime=findViewById(R.id.tv_alarm_set_day_time);
Intent intent=getIntent();
int setDay=intent.getIntExtra("setDay",0);
String day="";
switch (setDay){
case 0:
day="err";
break;
case 1:
day="일요일";
break;
case 2:
day="월요일";
break;
case 3:
day="화요일";
break;
case 4:
day="수요일";
break;
case 5:
day="목요일";
break;
case 6:
day="금요일";
break;
case 7:
day="토요일";
break;
default:
day="err";
break;
}
String setTime=day+"요일 \n"+"알람 셋팅 시간"+intent.getStringExtra("setTime");
alarmSetTime.setText(setTime);
}// onCreate()..
}// AlarmTimeViewActivity class..
|
BroadcastReceiver 를 상속받은 Alarm class에서 새로 만든 액티비티를 실행합니다.
Alarm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.example.ex_alarm;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Alarm extends BroadcastReceiver {
static String TAG="Alarm";
@Override
public void onReceive(Context context, Intent intent) {
boolean[] week = intent.getBooleanArrayExtra("weekday");
Log.d(TAG,week.toString());
Calendar cal = Calendar.getInstance();
SimpleDateFormat format1 = new SimpleDateFormat ( "HH:mm");
Date time = new Date();
String time1 = format1.format(time);
Intent intent2= new Intent(context, AlarmTimeViewActivity.class);
intent2.putExtra("setDay",cal.get(Calendar.DAY_OF_WEEK));
intent2.putExtra("setTime",time1.toString());
intent2.setFlags(intent2.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent2);
Toast.makeText(context, "알람~!!", Toast.LENGTH_SHORT).show();
}// onReceive()..
}// Alarm class..
|
<실행 화면> 요일과 시간을 선택하여 알림을 받도록 했습니다.
반응형
'안드로이드 > 개발자 일상' 카테고리의 다른 글
안드로이드 생명주기 (LifeCycle) (4) | 2020.04.08 |
---|---|
안드로이드 Retrofit2+RecyclerView 1-2 (추가 밀어서 삭제,편집 ) (5) | 2020.04.06 |
안드로이드 AlarmManager 1-1 (매일 반복적으로 알람 받기) (2) | 2020.04.02 |
안드로이드 CircleProgressBar (원형 프로그래스 바, 라이브러리 사용) (13) | 2020.03.29 |
안드로이드 EditText (빨간 테두리 색 적용, 이메일 형식 체크) (4) | 2020.03.29 |
댓글