326
No
class MainActivity
package com.example.choi.ex10_service;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View v){
Intent intent=null;
switch (v.getId()){
case R.id.button1:
intent=new Intent(this, AlarmActivity.class);
break;
}
startActivity(intent);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.ex10_service.MainActivity">
<Button
android:text="알람서비스"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/button1"
android:onClick="onClick"
/>
</RelativeLayout>
class AlarmReceiver
package com.example.choi.ex10_service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
//BroadcastReceiver : 방송 수신기(서비스 요청에 대한 응답 처리)
public class AlarmReceiver extends BroadcastReceiver {
public AlarmReceiver() {
}
//알람 통지 => onReceive()에서 처리
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "시간이 되었습니다."
, Toast.LENGTH_SHORT).show();
}
}
class AlarmActivity
package com.example.choi.ex10_service;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.Calendar;
public class AlarmActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
}
public void onClick(View v){
//알람 관리자
//getSystemService(시스템서비스) : 안드로이드 시스템에서 제공하는 기본서비스
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=null;
//PendingIntent : Pending - 계류중인 , 미정, 미결
PendingIntent sender=null;
Calendar calendar =null;
switch (v.getId()){
case R.id.button1: //1회 알람
Toast.makeText(AlarmActivity.this, "1회 알람"
, Toast.LENGTH_SHORT).show();
//알람 시간이 되면 브로드 캐스트가 리시버 발송 =>AlarmReceiver 가 수신
intent=new Intent(AlarmActivity.this, AlarmReceiver.class);
//getBroadcast(컨텍스트, 요청코드, 인텐트, 플래그)
sender=PendingIntent.getBroadcast(this, 0, intent, 0);
//import java.util.Calendar;
calendar =Calendar.getInstance();
//현재 시간 저장
calendar.setTimeInMillis(System.currentTimeMillis());
long currentTime=calendar.getTimeInMillis();
//현재 시간을 기준으로 3초 경과 후 알람
calendar.add(Calendar.SECOND, 3);
//지정된 시간에 알람 처리
//calendar.set(Calendar.HOUR, 24);
// calendar.set(Calendar.MINUTE,1);
// calendar.set(Calendar.SECOND,0);
long alarmTime=calendar.getTimeInMillis();
Log.i("message", currentTime + " : " +alarmTime);
if(alarmTime > currentTime){
//알람 설정 alarManager.set( 타임옵션, 알람시간, 예약인텐트)
//5초 후에 예약인텐트 실행
//Real Time Clock : 컴퓨터 시간
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), sender);
}
break;
case R.id.button2://반복 알람
//캘린더 객체 생성
calendar=Calendar.getInstance();
//캘린더 시간 설정
calendar.setTimeInMillis(System.currentTimeMillis());
//알람 시간이 되었을 때 처리될 예약인텐트 설정
intent=new Intent(this, AlarmReceiver.class);
sender=PendingIntent.getBroadcast(this, 0, intent, 0);
//반복 알람 설정(안드로이드 5.1 부터 배터리 소모 문제로 최저 1분으로 설정)
alarmManager.setRepeating(AlarmManager.RTC,
System.currentTimeMillis(), 1000*60,sender);
break;
case R.id.button3://반복 알람 중지
intent=new Intent(this, AlarmReceiver.class);
sender=PendingIntent.getBroadcast(this, 0,intent,0);
alarmManager.cancel(sender);
break;
}
}
}
alarm.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/alarm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.ex10_service.AlarmActivity">
<Button
android:text="1회 알람"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
android:layout_marginTop="24dp"
android:id="@+id/button1" />
<Button
android:text="반복 알람"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:id="@+id/button2"
android:onClick="onClick"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="반복 알람 중지"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="onClick"
/>
</RelativeLayout>
댓글 ( 5)
댓글 남기기