Android控件之Notification

<span style="font-size:18px;">package com.example.notificationtest;

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

public class MainActivity extends Activity implements OnClickListener {
	private Button btn_notification1;
	private Button btn_notification2;
	private Button btn_notification3;
	private Button btn_notification4;
	private Button btn_notification5;
	private static final int NOTIFICATION_FLAG = 1;
	NotificationManager manager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		btn_notification1 = (Button) findViewById(R.id.btn_notivication1);
		btn_notification2 = (Button) findViewById(R.id.btn_notivication2);
		btn_notification3 = (Button) findViewById(R.id.btn_notivication3);
		btn_notification4 = (Button) findViewById(R.id.btn_notivication4);
		btn_notification5 = (Button) findViewById(R.id.btn_notivication5);
		
		manager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
		 
		btn_notification1.setOnClickListener(this);
		btn_notification2.setOnClickListener(this);
		btn_notification3.setOnClickListener(this);
		btn_notification4.setOnClickListener(this);
		btn_notification5.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_notivication1:
			sendNotification1();
			break;
		case R.id.btn_notivication2:
			sendNotification2();
			break;
		case R.id.btn_notivication3:
			sendNotification3();
			break;
		case R.id.btn_notivication4:
			sendNotification4();
			break;
		case R.id.btn_notivication5:
			clearNotification();
			break;
		default:
			break;
		}
	}
	/**
</span><h1><span style="font-size:18px;">	 * </span><span style="font-size:32px;">清除通知</span></h1><span style="font-size:18px;">	 */
	private void clearNotification() {
		// TODO Auto-generated method stub
		// 清除id为NOTIFICATION_FLAG的通知
		manager.cancel(NOTIFICATION_FLAG);
		// 清除所有的通知
		// manager.cancelAll();
	}
	/**
</span><h1><span style="font-size:18px;">	 * </span><span style="font-size:32px;">自定义通知</span></h1><span style="font-size:18px;">	 */
	private void sendNotification4() {
		// TODO Auto-generated method stub
		// Notification myNotify = new Notification(R.drawable.message,
		// "自定义通知:您有新短信息了,请注意查收!", System.currentTimeMillis());
		Notification myNotify = new Notification();
		myNotify.icon = R.drawable.ic_launcher;
		myNotify.tickerText = "TickerText:您有新短消息,请注意查收!";
		myNotify.when = System.currentTimeMillis();
		myNotify.flags = Notification.FLAG_NO_CLEAR;// 不能够自动清除
		RemoteViews rv = new RemoteViews(getPackageName(),
				R.layout.my_notification);
		rv.setTextViewText(R.id.textView, "hello wrold!");
		myNotify.contentView = rv;
		Intent intent = new Intent(Intent.ACTION_MAIN);
		PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
				intent, 1);
		myNotify.contentIntent = contentIntent;
		manager.notify(NOTIFICATION_FLAG, myNotify);
	}
	/**
</span><h1><span style="font-size:18px;">	 * </span><span style="font-size:32px;">默认通知,API16后可用</span></h1><span style="font-size:18px;">	 */
	private void sendNotification3() {
		// TODO Auto-generated method stub
		PendingIntent pendingIntent3 = PendingIntent.getActivity(this, 0,
				new Intent(this, MainActivity.class), 0);
		// 通过Notification.Builder来创建通知,注意API Level
		// API16之后才支持
		/**
		 * 需要注意build()是在APIlevel16及之后增加的,
		 * API11可以使用getNotificatin()来替代
		 */
		Notification notify3 = new Notification.Builder(this)
				.setSmallIcon(R.drawable.ic_launcher)
				.setTicker("TickerText:" + "您有新短消息,请注意查收!")
				.setContentTitle("Notification Title")
				.setContentText("This is the notification message")
				.setContentIntent(pendingIntent3).setNumber(1).build(); 
		// FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
		notify3.flags |= Notification.FLAG_AUTO_CANCEL;
		//通过通知管理器来发起通知。如果id不同,则每click,在status哪里增加一个提示
		manager.notify(NOTIFICATION_FLAG, notify3);
	}

	/**
</span><h1><span style="font-size:18px;">	 * </span><span style="font-size:32px;">默认通知,API11后可用</span></h1><span style="font-size:18px;">	 */
	private void sendNotification2() {
		// TODO Auto-generated method stub
		PendingIntent pendingIntent2 = PendingIntent.getActivity(this, 0,
				new Intent(this, MainActivity.class), 0);
		// 通过Notification.Builder来创建通知
		/**
		 *  设置状态栏中的小图片,尺寸一般建议在24×24,这个图片
		 *  同样也是在下拉状态栏中所显示,如果在那里需要更换更大的图片,
		 *  可以使用setLargeIcon(Bitmapicon)
		 */	
		Notification notify2 = new Notification.Builder(this)
				.setSmallIcon(R.drawable.ic_launcher) 
				// 设置在statusbar上显示的提示文字
				.setTicker("TickerText:" + "您有新短消息,请注意查收!")
				// 设置在下拉statusbar后Activity,本例子中
				//的NotififyMessage的TextView中显示的标题
				.setContentTitle("Notification Title")
				// TextView中显示的详细内容
				.setContentText("This is the notification message")
				// 关联PendingIntent
				.setContentIntent(pendingIntent2) 
				 /**
				  * 在TextView的右方显示的数字,可放大图片看,在最右侧。
				  * 这个number同时也起到一个序列号的左右,如果多个触发多
				  * 个通知(同一ID),可以指定显示哪一个。
				  */			
				.setNumber(1) 
				// 需要注意build()是在API level
				.getNotification(); 
		// 16及之后增加的,在API11中可以使用getNotificatin()来代替
		notify2.flags |= Notification.FLAG_AUTO_CANCEL;
		manager.notify(NOTIFICATION_FLAG, notify2);
	}

	/**
</span><h1><span style="font-size:18px;">	 * </span><span style="font-size:32px;">默认通知(已被抛弃,但是通用)</span></h1><span style="font-size:18px;">	 */
	private void sendNotification1() {
		// TODO Auto-generated method stub
		/**
		 * 创建一个PendingIntent,和Intent类似,不同的
		 * 是由于不是马上调用,需要在下拉状态条出发的activity,
		 * 所以采用的是PendingIntent,即点击Notification跳
		 * 转启动到哪个Activity
		 */	 
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, MainActivity.class), 0);
		// 下面需兼容Android 2.x版本是的处理方式
		// Notification notify1 = new Notification(R.drawable.message,
		// "TickerText:" + "您有新短消息,请注意查收!", System.currentTimeMillis());
		Notification notify1 = new Notification();
		notify1.icon = R.drawable.ic_launcher;
		notify1.tickerText = "TickerText:您有新短消息,请注意查收!";
		notify1.when = System.currentTimeMillis();
		notify1.setLatestEventInfo(this, "Notification Title",
				"This is the notification message", pendingIntent);
		notify1.number = 1;
		// FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
		notify1.flags |= Notification.FLAG_AUTO_CANCEL; 
		// 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示
		manager.notify(NOTIFICATION_FLAG, notify1);
	}
}</span>


附(自定义通知布局)

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
</LinearLayout></span>



转至: http://blog.csdn.net/loongggdroid/article/details/17616509

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值