본문 바로가기

JAVA/Android

안드로이드 알람(Alarm,Notification) 예제 따라하기

- 해당 시간이 되면 통지가 되는 알람 기능 기능의 간단한 예제이다.
- 이 예제를 통해 알람 App을 응용해서 만들어 볼 수 있다.

-Java 소스 
public class Ex15_AlarmActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button btn1 = (Button) this.findViewById(R.id.button1);
btn1.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlarmManager alarm = (AlarmManager) this
.getSystemService(Context.ALARM_SERVICE);

switch (arg0.getId()) {
case R.id.button1:
Intent intent = new Intent(Ex15_AlarmActivity.this,
AlarmReceive.class);
PendingIntent pender = PendingIntent.getBroadcast(
Ex15_AlarmActivity.this, 0, intent, 0);

EditText edit1 = (EditText) this.findViewById(R.id.edit1);
int year = Integer.valueOf(edit1.getText().toString());
EditText edit2 = (EditText) this.findViewById(R.id.edit2);
int month = Integer.valueOf(edit2.getText().toString());
EditText edit3 = (EditText) this.findViewById(R.id.edit3);
int day = Integer.valueOf(edit3.getText().toString());
EditText edit4 = (EditText) this.findViewById(R.id.edit4);
int hour = Integer.valueOf(edit4.getText().toString());
EditText edit5 = (EditText) this.findViewById(R.id.edit5);
int minute = Integer.valueOf(edit5.getText().toString());

Calendar calendar = Calendar.getInstance();
calendar.set(year, month - 1, day, hour, minute);

alarm.set(AlarmManager.RTC, calendar.getTimeInMillis(), pender);

/*
* Toast.makeText(this, calendar.get(Calendar.YEAR)+
* "/"+calendar.get(Calendar.MONTH)+
* "/"+calendar.get(Calendar.DAY_OF_MONTH)+
* "/"+calendar.get(Calendar.HOUR)+
* "/"+calendar.get(Calendar.MINUTE) , Toast.LENGTH_SHORT).show();
*/
break;
}

}

-------------------------------------------------------------------------------------------------------
public class AlarmReceive extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Toast.makeText(context, "Alarm Received!", Toast.LENGTH_LONG).show();

NotificationManager notifier = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

Notification notify = new Notification(R.drawable.icon, "text",
System.currentTimeMillis());

Intent intent2 = new Intent(context, Ex15_AlarmActivity.class);
PendingIntent pender = PendingIntent
.getActivity(context, 0, intent2, 0);

notify.setLatestEventInfo(context, "alimtitle", "hackjang", pender);

notify.flags |= Notification.FLAG_AUTO_CANCEL;
notify.vibrate = new long[] { 200, 200, 500, 300 };
// notify.sound=Uri.parse("file:/");
notify.number++;

notifier.notify(1, notify);

}

}

- xml 소스
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edit1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="년" />

        <EditText
            android:id="@+id/edit2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="월" />

        <EditText
            android:id="@+id/edit3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="일" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/edit4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="시" />

        <EditText
            android:id="@+id/edit5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="분" />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="알람설정" />

</LinearLayout>

-실행 화면