zoukankan      html  css  js  c++  java
  • android之【本地通知Notification】

    public class NotificationTest extends Activity
    {
    	static final int NOTIFICATION_ID = 0x1123;
    	@Override
    	public void onCreate(Bundle savedInstanceState)
    	{
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
    		//获取应用界面中的Button对象
    		Button bn = (Button) findViewById(R.id.bn);
    		//为button的单击事件绑定事件监听器
    		bn.setOnClickListener(new View.OnClickListener()
    		{
    			@Override
    			public void onClick(View source)
    			{
    				//创建一个启动其它Activity的Intent
    				Intent intent = new Intent(NotificationTest.this
    					, OtherActivity.class);
    				PendingIntent pi = PendingIntent.getActivity(NotificationTest.this
    					, 0, intent , 0);
    				//创建一个Notification
    				Notification notify = new Notification();
    				//为Notification设置图标,该图标显示在状态栏
    				notify.icon = R.drawable.notify;
    				//为Notification设置文本内容,该文本会显示在状态栏
    				notify.tickerText = "启动其它Activity的通知";
    				//为Notification设置发送时间
    				notify.when = System.currentTimeMillis();
    				//为Notification设置声音
    				notify.defaults = Notification.DEFAULT_SOUND;
    				//为Notification设置默认声音、默认振动、默认闪光灯
    				notify.defaults = Notification.DEFAULT_ALL;
    				//设置事件信息
    				notify.setLatestEventInfo(NotificationTest.this, "普通通知",
    					"点击查看", pi);
    				//获取系统的NotificationManager服务
    				NotificationManager notificationManager = (NotificationManager) 
    					getSystemService(NOTIFICATION_SERVICE);
    				//发送通知
    				notificationManager.notify(NOTIFICATION_ID, notify);
    			}
    		});
    		
    		//取消通知
    		Button del = (Button)findViewById(R.id.del);
    		del.setOnClickListener(new OnClickListener()
    		{
    			@Override
    			public void onClick(View v)
    			{
    				//获取系统的NotificationManager服务
    				NotificationManager notificationManager = (NotificationManager) 
    					getSystemService(NOTIFICATION_SERVICE);
    				//取消通知
    				notificationManager.cancel(NOTIFICATION_ID);
    			}
    		});
    	}
    }

  • 相关阅读:
    就为了一个原子操作,其他CPU核心罢工了
    浅谈JVM和垃圾回收
    简单了解一下K8S,并搭建自己的集群
    WebAssembly完全入门——了解wasm的前世今身
    【简单了解系列】从基础的使用来深挖HashMap
    【俗话说】换个角度理解TCP的三次握手和四次挥手
    两分钟让你明白Go中如何继承
    游戏服务器和Web服务器的区别
    Go中使用seed得到相同随机数的问题
    从web到游戏,走出舒适区
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4248612.html
Copyright © 2011-2022 走看看