Notification各种用法源码详解

Notification和NotificationManage
最新API
创建Notification:
1.通过NotificationCompat.Builder对象来指定UI信息和事件
2.NotificationCompat.Builder.build()来创建Notification对象,其中包含了你指定的信息和事件
3.通过NotificationManager.notify()来发布你的Notification对象
Notification Action:
1.定义一个包含Intent的PendingIntent
2.通过NotificationCompat.Builder对象的setContentIntent().来给Notification添加PendingIntent
代码如下:
大视图通知栏:
1.通过NotificationCompat.Builder对象来指定UI信息和事件
2.NotificationCompat.InboxStyle 对象
3.设置NotificationCompat.InboxStyle 对象的信息(setBigContentTitle标题.../addLine()每行的内容)
4.mBuilder.setStyle(inBoxStyle);//调用setStyle()方法给NotificationCompat.Builder对象设置大图标(抽屉)类型
显示进度的通知:
A.确定时间进度的进度指示器:
1.通过NotificationCompat.Builder对象来指定UI信息和事件
2.调用setProgress(max, progress, false),然后发布通知
其中:max一般为100,progress为百分制值,
3.结束时可以保留进度的显示,或使用重新记录同时文本(比如下载完成),然后调用setprogress(0.0.false).

进行及完成后的效果效果:




B.不确定时间的进度指示器:
1.add it to your notification with setProgress(0, 0, true),前两个参数无效
2.发布通知
3.使用重新记录同时文本(比如下载完成),然后调用setprogress(0.0.false),和上面的有点不同,这一步一定要做,否则进度条会一直滚动

效果:


源码如下:

package com.example.notifications;

import java.util.Currency;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.RemoteViews;

public class MainActivity extends Activity {

	private static final String TAG = "_-->>>>>>";
	private static final RemoteViews CurrentText = null;
	int notificationID = 1;

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

	public void onClick(View view) {

		displayNotification();
	}

	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	protected void displayNotification() {

		Intent i = new Intent(this, NotificationView.class);
		i.putExtra("notificationID", notificationID);
		// ******************************************一般情况下的做法******************************************
		/*
		 * PendingIntent pendingIntent = PendingIntent.getActivity(this,
		 * notificationID, i, 0);
		 */

		// **********************************************设置点击后返回一定是home********************************
		// 当你点击notification后打开的界面是应用流程的一部分,需要这一块,能直接回到home,如果只是显示notification
		// 的详细信息,可以直接使用上面的一般情况下的做法
		TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
		// Adds the back stack for the Intent (but not the Intent itself)
		stackBuilder.addParentStack(NotificationView.class);
		// Adds the Intent that starts the Activity to the top of the stack
		stackBuilder.addNextIntent(i);
		PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
				PendingIntent.FLAG_UPDATE_CURRENT);
		// ****************************************************************************************************

		final NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		// api 11是这种方法
		// Notification notif = new Notification(R.drawable.ic_launcher,
		// "Remingder:Meeting starts in 5 minutes",
		// System.currentTimeMillis());
		CharSequence from = "系统闹钟";
		CharSequence message = "和客户开会在下午3点,请提前到会场...";

		final NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(
				this).setContentIntent(pendingIntent)
				.setSmallIcon(R.drawable.haitao)
				//.setWhen(System.currentTimeMillis())
				.setContentTitle(from)
				.setContentText(message).setAutoCancel(true);
		// *******************************设置通知栏的大视图big view,android
		// 4.1之后支持**********************

		/*
		 * NotificationCompat.InboxStyle inboxStyle = new
		 * NotificationCompat.InboxStyle(); String[] events = new
		 * String[]{"这里","写的","是","一些","详细","内容"};
		 * inboxStyle.setBigContentTitle("(大视图的名称)详细内容为:"); // 大视图的名称
		 * inboxStyle.setSummaryText("写通知概要的地方"); for (int a = 0; a <
		 * events.length; a++) { inboxStyle.addLine(events[a]); }
		 * nBuilder.setStyle(inboxStyle);
		 */

		// *************************************************************************************************
		// *******************************************更新通知条数信息******************************************
		int numMessage = 0;
		nBuilder.setContent(CurrentText).setNumber(++numMessage);
		// ********************************************************************************************
		// ***********************明确进度时间的进度通知栏显示**********************
/*		new Thread(new Runnable() {
			public void run() {
				int incr;
				for (incr = 0; incr <= 100; incr += 5) {
					nBuilder.setProgress(100, incr, false);
					nm.notify(0, nBuilder.build());
					try {
						// Sleep for 5 seconds
						Thread.sleep(1 * 1000);
					} catch (InterruptedException e) {
						Log.d(TAG, "sleep failure");
					}
				}
				nBuilder.setContentText("开始开会");
				nBuilder.setProgress(0, 0, false);
				nm.notify(0, nBuilder.build());
			}

		}).start();*/
		// **********************************************************
		//***********************不明确进度时间的进度通知栏************************
		new Thread(new Runnable() {
			public void run() {
				for (int incr = 0; incr <= 100; incr += 5) {
					nBuilder.setProgress(0, 0, true);
					nm.notify(0, nBuilder.build());
					try {
						// Sleep for 5 seconds
						Thread.sleep(1 * 1000);
					} catch (InterruptedException e) {
						Log.d(TAG, "sleep failure");
					}
				}
				nBuilder.setContentText("开始开会");
				nBuilder.setProgress(0, 0, false);
				nm.notify(0, nBuilder.build());
			}

		}).start();
		//**************************************************************************
		// nm.notify(notificationID, nBuilder.build());

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值