※ 알람의 종류
- 1회만 발생
- 주기적으로 발생(별도의 stop기능을 구현하지 않으면 지속적으로 울린다)
※ 구현
MainActivity
public class MainActivity extends Activity {
Button btnOneAlarm;
Button btnCycleAlarm;
Button btnStopAlarm;
AlarmManager alarmManger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOneAlarm = (Button) findViewById(R.id.bt_start_one_alarm);
btnCycleAlarm = (Button) findViewById(R.id.bt_start_cycle_alarm);
btnStopAlarm = (Button) findViewById(R.id.bt_stop_cycle_alarm);
btnOneAlarm.setOnClickListener(listener);
btnCycleAlarm.setOnClickListener(listener);
btnStopAlarm.setOnClickListener(listener);
alarmManger = (AlarmManager) getSystemService(ALARM_SERVICE);
}
private void oneAlarm(){
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("str", "value");
//앱이 종료되어도 알람이 울려야 하므로 사용한다.
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
//일회성에선 set을 사용
alarmManger.set(AlarmManager.RTC, System.currentTimeMillis()+3000, pendingIntent);
}
private void cycleAlam(){
Intent intent = new Intent(MainActivity.this, MainActivity.class);
intent.putExtra("str", "value");
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
//cycle에선 setRepeating을 사용 3초후에 알림이 울리고 주기적으로 5초 간격으로 알람이 울린다.
alarmManger.setRepeating(AlarmManager.RTC, System.currentTimeMillis()+3000,5000, pendingIntent);
}
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bt_start_one_alarm:
oneAlarm();
break;
case R.id.bt_start_cycle_alarm:
cycleAlam();
break;
case R.id.bt_stop_cycle_alarm:
Intent intent = new Intent(MainActivity.this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
alarmManger.cancel(pendingIntent);
break;
default:
break;
}
}
SecondActivity
public class SecondActivity extends Activity {
AlarmManager alarmManger;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
alarmManger = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = getIntent();
String s = intent.getStringExtra("str");
TextView tv = (TextView) findViewById(R.id.tv_01);
tv.setText(s);
}
}
activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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.android_20_2_ex1.MainActivity" >
<Button
android:id="@+id/bt_start_one_alarm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="One Alarm" />
<Button
android:id="@+id/bt_start_cycle_alarm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cycle Alarm" />
<Button
android:id="@+id/bt_stop_cycle_alarm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Stop Cycle Alarm" />
</LinearLayout>
activity_second
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
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.android_20_2_ex1.SecondActivity" >
<TextView
android:id="@+id/tv_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10px"
android:textSize="20px"
android:textStyle="bold"
android:background="#00ff00"
android:textColor="#ffffff"
android:gravity="center" />
</LinearLayout>
'플밍 is 뭔들 > 안드로이드_인강' 카테고리의 다른 글
13-2. 스레드 (0) | 2016.11.28 |
---|---|
13-1. 스레드 (0) | 2016.11.28 |
12-1. 노티(Notification)바 (0) | 2016.11.27 |
11-3. 팝업대화상자 (0) | 2016.11.27 |
11-2. 팝업대화상자 (0) | 2016.11.27 |