实现通知栏消息的生成和消除

  1. MainActivity.java  
  2.     
  3. public class MainActivity extends Activity  
  4. {  
  5.     static final int NOTIFICATION_ID = 0x123;  
  6.     NotificationManager nm;  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState)  
  9.     {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.main);  
  12.         // 获取系统的NotificationManager服务  
  13.         nm = (NotificationManager)  
  14.                 getSystemService(NOTIFICATION_SERVICE);  
  15.     }  
  16.     // 为发送通知的按钮的点击事件定义事件处理方法  
  17.     public void send(View source)  
  18.     {  
  19.         // 创建一个启动其他Activity的Intent  
  20.         Intent intent = new Intent(MainActivity.this  
  21.                 , OtherActivity.class);  
  22.         PendingIntent pi = PendingIntent.getActivity(  
  23.                 MainActivity.this, 0, intent, 0);  
  24.         Notification notify = new Notification.Builder(this)  
  25.                 // 设置打开该通知,该通知自动消失  
  26.                 .setAutoCancel(true)  
  27.                 // 设置显示在状态栏的通知提示信息  
  28.                 .setTicker("有新消息")  
  29.                 // 设置通知的图标  
  30.                 .setSmallIcon(R.drawable.notify)  
  31.                 // 设置通知内容的标题  
  32.                 .setContentTitle("一条新通知")  
  33.                 // 设置通知内容  
  34.                 .setContentText("恭喜你,您加薪了,工资增加20%!")  
  35.                 // 设置使用系统默认的声音、默认LED灯  
  36.                 // .setDefaults(Notification.DEFAULT_SOUND  
  37.                 // |Notification.DEFAULT_LIGHTS)  
  38.                 // 设置通知的自定义声音  
  39.                 .setSound(Uri.parse("android.resource://org.crazyit.ui/"  
  40.                         + R.raw.msg))  
  41.                 .setWhen(System.currentTimeMillis())  
  42.                 // 设改通知将要启动程序的Intent  
  43.                 .setContentIntent(pi)  // ①  
  44.                 .build();  
  45.         // 发送通知  
  46.         nm.notify(NOTIFICATION_ID, notify);  
  47.     }  
  48.     // 为删除通知的按钮的点击事件定义事件处理方法  
  49.     public void del(View v)  
  50.     {  
  51.         // 取消通知  
  52.         nm.cancel(NOTIFICATION_ID);  
  53.     }  
  54. }  
  55.     
  56.     
  57. OtherActivity  
  58.     
  59. public class OtherActivity extends Activity  
  60. {  
  61.    @Override  
  62.    public void onCreate(Bundle savedInstanceState)  
  63.    {  
  64.       super.onCreate(savedInstanceState);  
  65.       //设置该Activity显示的页面  
  66.       setContentView(R.layout.other);  
  67.    }  
  68. }  
  69.     
  70. XML文件  
  71.     
  72. <?xml version="1.0" encoding="utf-8"?>  
  73. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  74.    android:orientation="horizontal"  
  75.    android:layout_width="match_parent"  
  76.    android:layout_height="match_parent"  
  77.    android:gravity="center_horizontal">  
  78. <Button  
  79.    android:layout_width="wrap_content"   
  80.    android:layout_height="wrap_content"   
  81.    android:text="发送Notification"  
  82.    android:onClick="send"  
  83.    />  
  84. <Button  
  85.    android:layout_width="wrap_content"   
  86.    android:layout_height="wrap_content"   
  87.    android:text="删除Notification"  
  88.    android:onClick="del"  
  89.    />   
  90. </LinearLayout>  
  91.     
  92. other.xml  
  93.     
  94. <?xml version="1.0" encoding="utf-8"?>  
  95. <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
  96.    android:layout_width="match_parent"  
  97.    android:layout_height="wrap_content"  
  98.    android:gravity="center_horizontal"  
  99.    android:orientation="vertical">  
  100. <!-- 定义一个ImageView -->  
  101. <ImageView  
  102.    android:layout_width="match_parent"  
  103.    android:layout_height="wrap_content"  
  104.    android:src="@drawable/swift"  
  105.    android:layout_gravity="center_horizontal"  
  106.    />  
  107. </LinearLayout>  
  108.     
  109. Menu.xml  
  110.     
  111. <menu xmlns:android="http://schemas.android.com/apk/res/android"  
  112.      xmlns:tools="http://schemas.android.com/tools"  
  113.      tools:context=".MainActivity">  
  114.    <item android:id="@+id/action_settings"  
  115.         android:title="@string/app_name"  
  116.         android:orderInCategory="100"  
  117.         android:showAsAction="never"/>  
  118. </menu>  

效果

Notification的功能和用法 加薪通知的更多相关文章

  1. Notification的功能与用法

    Notification是显示在手机状态的通知——手机状态栏位于手机屏幕的最上方,那里一般显示了手机当前的网络状态.时间等.Notification所代表的是一种具有全局效果的通知,程序一般通过Not ...

  2. 2.6.2 Notification的功能与用法

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...

  3. Javascript自定义事件功能与用法实例分析

    原文地址:https://www.jb51.net/article/127776.htm 本文实例讲述了javascript自定义事件功能与用法.分享给大家供大家参考,具体如下: 概述 自定义事件很难 ...

  4. Android 自学之画廊视图(Gallery)功能和用法

    Gallery与之前讲的Spinner有共同的父类:AbsSpinner,表明Gallery和Spinner都是一个列表框.他们之间的区别在于Spinner显示的是一个垂直的列表框,而Gallery显 ...

  5. 搜索框(SearchView)的功能与用法

    SearchView是搜索框组件,它可以让用户在文本框内输入汉字,并允许通过监听器监控用户输入,当用户用户输入完成后提交搜索按钮时,也通过监听器执行实际的搜索. 使用SearchView时可以使用如下 ...

  6. 数值选择器(NumberPicker)的功能与用法

    数值选择器用于让用户输入数值,用户既可以通过键盘输入数值,也可以通过拖动来选择数值.使用该组件常用如下三个方法. setMinValue(int minVal):设置该组件支持的最小值. setMax ...

  7. 日历视图(CalendarView)组件的功能和用法

    日历视图(CalendarView)可用于显示和选择日期,用户既可选择一个日期,也可通过触摸来滚动日历.如果希望监控该组件的日历改变,可调用CalendarView的setOnDateChangeLi ...

  8. 星级评分条(RatingBar)的功能和用法

    星级评分条与拖动条有相同的父类:AbsSeekBar,因此它们十分相似.实际上星级评分条与拖动条的用法.功能都十分接近:它们都是允许用户通过拖动条来改变进度.RatingBar与SeekBar最大区别 ...

  9. 拖动条(SeekBar)的功能和用法

    拖动条和进度条非常相似,只是进度条采用颜色填充来表明进度完成的程序,而拖动条则通过滑块的位置来标识数值——而且拖动条允许用户拖动滑块来改变值,因而拖动条通常用于对系统的某种数值进行调节,比如调节音量等 ...

随机推荐

  1. mysql insert on duplicate key, update, ignore

    insert 语句中不能使用where,所以如果需要根据插入的数据在已有的数据库表是否重复做一些操作可以使用下面三种方法: 1. 使用insert,捕获duplicate错误 2. insert in ...

  2. SpringBoot application.yml文件不生效

    yml格式对缩进有严格的要求,检查你的yml配置文件是否有不合格的缩进项. 正确的格式如下: server: port: 8881 port前必须有空格,  port后的冒号 后面也需要有空格

  3. Java 静态代理和动态代理例子

    代理Proxy: Proxy代理模式是一种结构型设计模式,主要解决的问题是:在直接访问对象时带来的问题 代理是一种常用的设计模式,其目的就是为其他对象提供一个代理以控制对某个对象的访问.代理类负责为委 ...

  4. CSS中不透明度继承问题的处理

    关于CSS中不透明度的设置,除了兼容方面的问题,还有不透明度继承问题,这里只讨论下后者. 那么, 什么时候会发生不透明度继承问题? 当文档结构中有父子嵌套关系的时候,并且父元素有不透明度属性设置时,会 ...

  5. shell中的条件判断以及与python中的对比

    shell中比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试. 注意:这里的空格很重要.要确保方括号的空格. if ....; then          ...

  6. sqlserver字符串拆分函数

    CREATE FUNCTION f_splitSTR(@s varchar(8000), --待分拆的字符串@split varchar(10) --数据分隔符)RETURNS @re TABLE(c ...

  7. Recursion递归

    /*java.lang 核心包 如 String Math Integer System Thread等 拿来直接用 * java.awt 窗口工具 GUI * java.net 网络包 * java ...

  8. 集成学习算法总结----Boosting和Bagging(转)

    1.集成学习概述 1.1 集成学习概述 集成学习在机器学习算法中具有较高的准去率,不足之处就是模型的训练过程可能比较复杂,效率不是很高.目前接触较多的集成学习主要有2种:基于Boosting的和基于B ...

  9. httpclient4例子

    参考:http://hc.apache.org/httpclient-3.x/tutorial.html import org.apache.http.HttpEntity; import org.a ...

  10. Android 最火开发框架 xUtils

    xUtils简介 xUtils3 api变化较多, 已转至 https://github.com/wyouflf/xUtils3 xUtils 2.x对Android 6.0兼容不是很好, 请尽快升级 ...