android之————通知——notification

1、对于一个通知而言,它显示的消息是有限的,一般仅用于提示一些概要信息。

2、但是一般简短的消息,并不能表达需要告诉用户的全部内容,所以需要绑定一个意图,当用户点击通知的时候,调用一个意图展示出一个Activity用来显示详细的内容。

3、而Notification中,并不使用常规的Intent去传递一个意图,而是使用PendingIntent。

4、NotificationManager是所有Notification的大管家,它的主要职责是加入/移除Notification。

通过获取系统服务来获取该对象:

      NotificationManager mNotificationManager = (NotificationManager)getSystemServic(Context.NOTIFICATION_SERVICE) ;

5、通知的一些常用属性:

均需要通知管理员的notifi()方法去刷新通知。

NotificationManager manger = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

/**
	 * 方法一:初次学习通知里面的属性,系统的界面通知>>模拟短信通知
	 */
	public void SystemNotificationTest() {
		// 构造通知,并设置通知里面在状态栏显示的提醒信息,显示给用户界面的图片、标题、内容、时间.点击就消失通知等....
		NotificationCompat.Builder builder = new NotificationCompat.Builder(
				this);
		builder.setSmallIcon(R.drawable.m3);
		builder.setContentTitle("短信通知");
		builder.setContentText("你有1条未读短信,请点击查看");
		// builder.setAutoCancel(true);// 点击消失
		builder.setOngoing(true);// 设置为不可清除模式,流氓式通知
		builder.setTicker("你有新消息了!");// 任务栏里提示
		int kk = RENEW_ID++;
		builder.setNumber(kk);// 显示几条信息
		builder.setWhen(System.currentTimeMillis());// 显示当前时间

		Intent intentService = new Intent(this, MyTabHost.class);
		startService(intentService);
		
		// 实例化PendingIntent的方法中的四个属性(上下文,不知道直接放0就行,Intent,直接用PendingIntent点,里面有4个属性);
		PendingIntent pending = PendingIntent.getActivity(this, 0,
				intentService, PendingIntent.FLAG_UPDATE_CURRENT);		
		// 将此通知设置进去,PendingIntent和Intent的属性一样,但是不是立即跳转,未决的
		builder.setContentIntent(pending);
		// 刷新通知,前面那个参数是int型的,版本更新用的
		manger.notify(RENEW_ID, builder.build());
	}

6、如果你想点击通知进去过后停留在APP内,就要用到下面几行代码:

// 停留在那个APP应用里
		TaskStackBuilder taskstack = TaskStackBuilder.create(this);
		taskstack.addParentStack(MyTabHost.class);
		taskstack.addNextIntent(nextIntent);
<activity android:name="com.example.testone012.TabActivity.MyTabHost" >
    <!--XML 文件里面也要添加  -->   
         <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.testone005.mybaseadapter.s001.MybaseActivity" />
</activity>

 

7、自定义一个通知,用到此类RemoteViews:

// ------自定义notification界面
		RemoteViews remoteView = new RemoteViews(getPackageName(),
				R.layout.activity_sharedpreferences_layout);
		builder.setContent(remoteView);
		//自定义通知里面按钮的事件处理
		remoteView.setOnClickPendingIntent(viewId, pendingIntent);


8、自定义进度条模拟下载的通知:

private Handler mHandler = new Handler();
public void pregressBarNotification() {
		final NotificationCompat.Builder progressBarActionBuilder = new NotificationCompat.Builder(
				this);
		progressBarActionBuilder.setTicker("XX已加入下载!")
				.setSmallIcon(R.drawable.m3).setContentTitle("XX正在下载...")
				.setContentText("正在下载,请稍后....")
				.setWhen(System.currentTimeMillis());
		progressBarActionBuilder.setAutoCancel(true);
		new Thread(new Runnable() {
			public void run() {
				for (int progress = 0; progress < 101; progress++) {
					try {
						Thread.sleep(100);
						progressBarActionBuilder.setProgress(100, progress,
								false);

						// 刷新消息最好放到主线程去执行
						mHandler.post(new Runnable() {
							public void run() {
								manger.notify(RENEW_ID_PROGRESSBAR,
										progressBarActionBuilder.build());
							}
						});

					} catch (InterruptedException e) {
						e.printStackTrace();
					}

				}
				// 刷新消息最好放到主线程去执行
				mHandler.post(new Runnable() {
					public void run() {
						progressBarActionBuilder.setProgress(0, 0, false);
						progressBarActionBuilder.setContentTitle("下载完成!");
						progressBarActionBuilder.setContentText("请点击安装");
						manger.notify(RENEW_ID_PROGRESSBAR,
								progressBarActionBuilder.build());
					}
				});
			}
		}).start();

		// //模拟下载完成后发布通知信息
		//
		// manger.notify(RENEW_ID_PROGRESSBAR,
		// progressBarActionBuilder.build());
		//

	}



 



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值