Notification(하나하나의 알림), 

       NotificationManager,  (알림 관리자)

       Intent, (알림을 누르면 실행할 기능)

       PendingIntent  (인텐트의 wrapper)



<?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" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>


permission을 줘야 한다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.gusfree.notification"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".NotificationActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


package com.gusfree.notification;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.DialogInterface;
import android.content.Intent;

import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class NotificationActivity extends Activity implements OnClickListener{

	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        findViewById(R.id.button1).setOnClickListener(this);
    }

	@Override
	public void onClick(View arg0){
		//1.알림 매니저 호출, android OS(platform)기능
		NotificationManager manager =
				(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
		
		//2.단말기 위쪽에 뜰 알림 만들기
		Notification noti = 
				new Notification(R.drawable.red,"알림왔어요",System.currentTimeMillis());
		
		//5.알림 누르면 실행할 기능 만들기
		Intent intent= new Intent();
		intent.setAction(Intent.ACTION_CALL);//permission
		intent.setData(Uri.parse("tel:0123456789"));
		
		PendingIntent pending=PendingIntent.getActivity(this, 0, intent, 0);
		
		//3.위쪽에 뜬 알림을 드래그 했을 때 나올 한칸의 내용
		noti.setLatestEventInfo(this, 
				"contentTitle : 전화 요청", 
				"contentText : 0123456789", 
				pending);//알림 누르면 실행할 기능 PendingIntent
		
		//4.매니저에게 알림을 띄우도록 한다.
		manager.notify(0,noti);
	}
}




'Android > 2012.04월 강좌' 카테고리의 다른 글

7일차 Thread 2  (0) 2012.05.04
7일차 Thread  (0) 2012.05.04
7일차 AnimationSet  (0) 2012.05.04
7일차 ScaleAnimation  (0) 2012.05.04
7일차 RotateAnimation(setFillAfter,setRepeatCount)  (0) 2012.05.04

+ Recent posts