Notification使用详解

Notification和Toast一样,都是在android中起到通知和提醒的功能。但他们的使用原理完全不同,Toast是一个控件,使用new创建,而Notification是通过NotificationManager来管理的。

显示效果如下:


主程序:

public class Main extends Activity implements OnClickListener
{
	private NotificationManager notificationManager;
	

	private void setDefaults(String tickerText, String contentTitle,
			String contentText, int id, int resId, int defaults)
	{
		Notification notification = new Notification(resId,
				tickerText, System.currentTimeMillis());
		//创建Notification第三步
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, Main.class), 0);

		//创建Notification第四步
		notification.setLatestEventInfo(this, contentTitle, contentText,
				contentIntent);
		notification.defaults = defaults;
		//创建Notification第五步
		notificationManager.notify(id, notification);
		

	}

	private void showNotification(String tickerText, String contentTitle,
			String contentText, int id, int resId)
	{
		//创建Notification第二步
		Notification notification = new Notification(resId,
				tickerText, System.currentTimeMillis());

		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				getIntent(), 0);

		notification.setLatestEventInfo(this, contentTitle, contentText,
				contentIntent);
		notificationManager.notify(id, notification);

	}

	public void onClick(View v)
	{

		switch (v.getId())
		{
			case R.id.btnSmile:
				showNotification("今天非常高兴", "今天考试得了全年级第一",
						"数学100分、语文99分、英语100分,yeah!", R.drawable.smile,
						R.drawable.smile);
				break;

			case R.id.btnWhy:
				showNotification("这是为什么呢?", "这道题为什么会出错呢?", "谁有正确答案啊.",
						R.drawable.why, R.drawable.why);
				break;
			case R.id.btnWrath:
				showNotification("今天心情不好", "也不知道为什么,这几天一直很郁闷.", "也许应该去公园散心了",
						R.drawable.why, R.drawable.wrath);
				break;
			case R.id.btnClear:
				// notificationManager.cancel(R.drawable.smile);
				// notificationManager.cancel(R.drawable.why);
				notificationManager.cancelAll();
				break;
			case R.id.btnRing:
				setDefaults("使用默认的声音", "使用默认的声音", "使用默认的声音", R.id.btnRing, R.drawable.smile,
						Notification.DEFAULT_SOUND);
			case R.id.btnVibrate:
				setDefaults("使用默认的震动", "使用默认的震动", "使用默认的震动", R.id.btnVibrate,
						R.drawable.smile, Notification.DEFAULT_VIBRATE);
			case R.id.btnLight:
				setDefaults("使用默认的Light", "使用默认的Light", "使用默认的Light", R.id.btnLight,
						R.drawable.smile, Notification.DEFAULT_LIGHTS);
			case R.id.btnRingAndVibrate:
				setDefaults("所有的都使用默认值", "所有的都使用默认值", "所有的都使用默认值",
						R.id.btnRingAndVibrate, R.drawable.smile,
						Notification.DEFAULT_ALL);
			
				break;

		}

	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//创建Notification第一步
		notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		Button btnSmile = (Button) findViewById(R.id.btnSmile);
		Button btnWhy = (Button) findViewById(R.id.btnWhy);
		Button btnWrath = (Button) findViewById(R.id.btnWrath);
		Button btnClear = (Button) findViewById(R.id.btnClear);
		Button btnRing = (Button) findViewById(R.id.btnRing);		
		Button btnVibrate = (Button) findViewById(R.id.btnVibrate);
		Button btnLight = (Button) findViewById(R.id.btnLight);
		Button btnRingAndVibrate = (Button) findViewById(R.id.btnRingAndVibrate);
		btnSmile.setOnClickListener(this);
		btnWhy.setOnClickListener(this);
		btnWrath.setOnClickListener(this);
		btnClear.setOnClickListener(this);
		btnRing.setOnClickListener(this);
		btnVibrate.setOnClickListener(this);
		btnLight.setOnClickListener(this);
		btnRingAndVibrate.setOnClickListener(this);
	}
}

布局文件:

<?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/btnSmile" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="我今天非常高兴"
		android:drawablePadding="10dp" android:drawableLeft="@drawable/smile" />
	<Button android:id="@+id/btnWhy" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="这是为什么呢?"
		android:drawablePadding="10dp" android:drawableLeft="@drawable/why" />
	<Button android:id="@+id/btnWrath" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="今天心情不好"
		android:drawablePadding="10dp" android:drawableLeft="@drawable/wrath" />

	<Button android:id="@+id/btnRing" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="使用默认的声音" />
	<Button android:id="@+id/btnVibrate" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="使用默认的震动" />
	<Button android:id="@+id/btnLight" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="使用默认的Light" />
	<Button android:id="@+id/btnRingAndVibrate"
		android:layout_width="wrap_content" android:layout_height="wrap_content"
		android:text="所有的都使用默认值" />
	<Button android:id="@+id/btnClear" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:text="清除通知" />
</LinearLayout>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值