Android学习之Notification

Notification可以理解为通知的意思一般用来显示广播信息 用Notification就必须要用到NotificationManager

想用Notification一般有三个步骤,如下所示

一般获得系统级的服务NotificationManager。

              调用Context.getSystemService(NOTIFICATION_SERVICE)方法即可返回NotificationManager实例

实例化Notification,并设置其属性

              用Notification构造函数 public Notification(int icon, CharSequence tickerText, long when)构造Notification实例 

通过NotificationManager发通知就OK了

              NotificationManager有两个方法:notify()发出通知  cancel(...)取消通知


下面通过一个代码实例来介绍一下Notification

   先初始化notificationManager和notification两个成员变量

 notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
 notification = new Notification(R.drawable.touxiang,"信息",System.currentTimeMillis());
   PS: Notification构造函数里传的参数就是这样显示的第一个参数是 图像,第二个是标题 :信息 ,第三个是系统时间 (见图一) 之所以在这说一下这个构造函数是想与下面的setLatestEventInfo(...)作个区别。

         

                                         (  图一   )                                                                                           (   图二    )

   布局就不贴了 是两个Button  看看发送按钮

 sendButton.setOnClickListener(new OnClickListener()
	{
		
		@Override
		public void onClick(View v)
		{
			Intent intent = new Intent(NotificationActivity.this,NotificationActivity.class);
		        PendingIntent pendingIntent = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0);
		        notification.setLatestEventInfo(NotificationActivity.this, "你的一条信息", "来自张三的信息", pendingIntent);
		        notificationManager.notify(ID,notification);
		        notificationManager.notify(ID+1, notification);
		}
	})

      setLatestEventInfo(...)里面所传的参数的效果图:(见图二)通知里面有两条完全一样的消息,是的 你没看错,这是因为我用notificationManager  notify(通知)了两次 而且ID不同,ID是int型是通知信息的标示符。虽然我上面两条信息是一模一样的,但由于ID的不同 , 所以Android还是会显示两条信息。
          在此说一下参数pendingIntent的在setLatestEventInfo里所扮演的角色,是啥子意思呢?pendingIntent可以在另外的地方执行,不是立即意图。当用户点击扩展通知的时候 pendingIntent意图才开始执行,例如图二 我点击其中一个消息后,立马就进入另外一个Activity...
       正如上面看到的那样,除了为notification设置图标,标题外还可以设置提示音,震动,闪光灯 详情请见我转的一片文章Notification使用详解.....

package com.study.android;

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;

public class MainActivity extends Activity {
	private Button startBtn;
	private Button cancelBtn;
	private static final int HELLO_ID = 1;
	NotificationManager mNotificationManager;
	Notification mNotification;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        startBtn = (Button)findViewById(R.id.startBtn);
        cancelBtn = (Button)findViewById(R.id.cancelBtn);
        
        // ① 获取NotificationManager的引用 
        String ns = Context.NOTIFICATION_SERVICE;
        mNotificationManager = (NotificationManager)this.getSystemService(ns);
        
        
        // ② 初始化Notification
        int icon = R.drawable.ic_launcher;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();
        mNotification = new Notification(icon,tickerText,when);
        mNotification.defaults = Notification.DEFAULT_ALL;
        mNotification.flags |= Notification.FLAG_NO_CLEAR;
        mNotification.flags |= Notification.FLAG_SHOW_LIGHTS;
        // ③ 定义notification的消息 和 PendingIntent
        Context context = this;
        CharSequence contentTitle ="My notification";
        CharSequence contentText = "Hello World";
        Intent notificationIntent = new Intent(this,MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,0 );
        mNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        
        // ④ 把封装好的notification传入NotificationManager
        
        
        // 开启通知
        startBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mNotificationManager.notify(HELLO_ID,mNotification);
				
			}
		});
        
        // 取消通知
        cancelBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mNotificationManager.cancel(HELLO_ID);
				
			}
		});
    }
}

代码中有创建Notification步骤,详情可以看一下。


今天的笔记就学到这里!



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值