본문 바로가기

나 어제 배웠다/Android

Android MessageView-Alert

▣ Android MessageView-Alert

1. main.xml 버튼 추가

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <Button
    android:id="@+id/b01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Notification발생"
    />
   
  <Button
    android:id="@+id/b02"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Alert발생"
    />
</LinearLayout>

 

2. NotificationTestActivity.java 변경

package com.pns.hjh.ui.notify;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NotificationTestActivity extends Activity {


 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  //Applications Framework 이용
  final NotificationManager nm = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
  //new Date(2010-1900,11,25).getTime()
  final Notification n = new Notification(R.drawable.icon,"알림드려요",System.currentTimeMillis());

  Button b = (Button)findViewById(R.id.b01);

  b.setOnClickListener(new View.OnClickListener() {
   public void onClick(View v) {
    n.number++;
    // TODO Auto-generated method stub
    //Intent 전달매체   
    //Activity 화면단위
    Intent i = new Intent(NotificationTestActivity.this,NotificationTestActivity.class);
    PendingIntent pi = PendingIntent.getActivity(NotificationTestActivity.this, 0, i, 0);

    n.setLatestEventInfo(NotificationTestActivity.this, "제목"+n.number, "안드로이드 어떤가요?"+n.number, pi);
    nm.notify(n.number, n);
   }
  });

  methodA();
 }

 void methodA(){
  Button b = (Button)findViewById(R.id.b02);
 //  제3클래스 방식으로 이벤트 처리

b.setOnClickListener(new ThirdHandler(this));
 }
}

class ThirdHandler implements View.OnClickListener{
 NotificationTestActivity nta;
  ThirdHandler(NotificationTestActivity nta){
  this.nta = nta;
 }
 public void onClick(View v){
  AlertDialog.Builder builder= new AlertDialog.Builder(nta);
  builder.setTitle("대화창제목");
  builder.setMessage("Alert창이 잘뜨는가?");
  builder.setNegativeButton("닫기", new DialogInterface.OnClickListener() {

   public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub
    //닫기만 하려면 내용을 기술 하지 않아도 됨
    //닫기 이벤트 작성
   }
  });
  builder.show();
 }
}

 

4. 실행결과