zoukankan      html  css  js  c++  java
  • Android---60---Notification 通知栏的简单使用

    Notification是显示在手机状态栏的通知 


    通过Notification.Builder类创建Notification对象。

    Notification.Builder经常用法:

    setDefaults ():设置通知LED灯、音乐、振动等

    setAutoCancle():设置点击通知后,状态栏自己主动删除通知

    setContentTitle():设置通知标题

    setContentText():设置通知内容

    setSmallcon():设置小图标

    setLargecon():设置大图标

    setTick():设置通知在状态栏的提示为本

    setContentIntent ():设置点击通知后将要启动的程序组件相应的PendingIntent

    setWhen ():设置通知公布的时间

    步骤:

    1.调用getSystemService(NOTIFICATION_SERVICE)方法获取系统的NotificationManager方法

    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


    2.创建一个Notification.Builder对象

    Notification.Builder builder = new Notification.Builder(MainActivity.this);


    3.为builder设置各种属性

    4.创建一个Notification对象
    Notification notification = builder.build();

    5.通过NotificationManager的notify方法发送Notification 
    manager.notify(ID, notification);

    Demo:

    Activity:

    public class MainActivity extends Activity {
    
    	Button send, del;
    	NotificationManager manager;
    	int ID = 0x123;
    
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		send = (Button) findViewById(R.id.send);
    		del = (Button) findViewById(R.id.del);
    
    		manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    
    		send.setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    
    				Intent intent = new Intent(MainActivity.this, other.class);
    				PendingIntent pi = PendingIntent.getActivity(MainActivity.this,
    						0, intent, 0);
    
    				Notification.Builder builder = new Notification.Builder(
    						MainActivity.this);
    				builder
    				// Notification notification = new
    				// Notification.Builder(MainActivity.this)
    
    				// 设置打开通知,该通知取消
    				.setAutoCancel(true)
    				// 设置通知提示信息
    						.setTicker("有新消息")
    						// 设置通知的图标
    						.setSmallIcon(R.drawable.pig)
    						// 设置通知的标题
    						.setContentTitle("不好了。!!")
    						// 设置通知的内容
    						.setContentText("你家猪跑了")
    						// 设置使用系统默认的声音、LED
    						.setDefaults(
    								Notification.DEFAULT_LIGHTS
    										| Notification.DEFAULT_SOUND)
    						// 设置通知公布时间
    						.setWhen(System.currentTimeMillis())
    						// 设置将要启动的活动
    						.setContentIntent(pi).build();
    
    				Notification notification = builder.build();
    
    				manager.notify(ID, notification);
    
    			}
    		});
    
    		del.setOnClickListener(new OnClickListener() {
    
    			@Override
    			public void onClick(View v) {
    				manager.cancel(ID);
    			}
    		});
    	}
    }


     

    点击发送通知:


     

    点击该通知会跳转到还有一个活动:

  • 相关阅读:
    【eoe资源】通过片段创建灵活的用户界面
    史上最全的Android开发索引帖
    临时记录
    五种开源协议的比较(BSD,Apache,GPL,LGPL,MIT)
    【转】深入探讨 Android 传感器
    Java Collections Framework Java集合框架List,Map,Set等全面介绍之概要篇
    谷歌 G1 android APK安装器 离线安装软件
    【转】請為你的 Android 程式加上 obfuscation 吧!
    【转】Android Toolchain与Bionic Libc
    用VirtualBox在XP环境下虚拟Ubuntu的过程
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5101073.html
Copyright © 2011-2022 走看看