android的几种“通知”方式简单实现(Notification&NotificationManager)

摘要:
我相信每个人都熟悉通知。当QQ上有消息或收到消息时,手机顶部会显示一条新消息,例如。我今天将记录几个通知的使用情况。话不多说,直接去代码。1.创建一个android项目我的新项目minSdkVersion=“11”,targetSdkVersion=“19”。也就是说,它支持3.0.2的最低版本。习惯性地打开项目清单文件AndroidManifest。xml,并添加一个权限:不添加它就无法完成。

  关于通知Notification相信大家都不陌生了,平时上QQ的时候有消息来了或者有收到了短信,手机顶部就会显示有新消息什么的,就类似这种。今天就稍微记录下几种Notification的用法。3.0以前的通知和3.0以后的通知是有些区别的。话不多说,直接上代码。

  1、新建一个android项目

    我新建项目的 minSdkVersion="11",targetSdkVersion="19"。也就是支持最低版本的3.0的。

  2、习惯性地打开项目清单文件AndroidManifest.xml,添加一个权限:<uses-permission android:name="android.permission.VIBRATE"/> 不添加不行的。

  3、在布局activity_main.xml中添加几个按钮,样子就大概这样,垂直排版的LinearLayout

具体代码

android的几种“通知”方式简单实现(Notification&amp;NotificationManager)第1张android的几种“通知”方式简单实现(Notification&amp;NotificationManager)第2张
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >

       <Button 
           android:id="@+id/btn_01"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="3.0以前版本的notification,用新的吧"
           android:onClick="click"
           />

       <Button 
           android:id="@+id/btn_02"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="大视图文本通知"
           android:onClick="click"
           />
       
       <Button 
           android:id="@+id/btn_03"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="大视图图片通知"
           android:onClick="click"
           />
       
       <Button 
           android:id="@+id/btn_04"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:text="进度条通知"
           android:onClick="click"
           />
       
</LinearLayout>
View Code

 android的几种“通知”方式简单实现(Notification&amp;NotificationManager)第3张

   4、MainActivity中的代码:

android的几种“通知”方式简单实现(Notification&amp;NotificationManager)第4张android的几种“通知”方式简单实现(Notification&amp;NotificationManager)第5张
  1 package com.xin.day_12_notificationdemo1;
  2 
  3 import java.util.Timer;
  4 import java.util.TimerTask;
  5 
  6 import android.app.Activity;
  7 import android.app.Notification;
  8 import android.app.NotificationManager;
  9 import android.app.PendingIntent;
 10 import android.content.Intent;
 11 import android.graphics.BitmapFactory;
 12 import android.os.Bundle;
 13 import android.support.v4.app.NotificationCompat;
 14 import android.support.v4.app.NotificationCompat.BigPictureStyle;
 15 import android.support.v4.app.NotificationCompat.BigTextStyle;
 16 import android.support.v4.app.NotificationCompat.Builder;
 17 import android.util.Log;
 18 import android.view.View;
 19 
 20 public class MainActivity extends Activity {
 21 
 22     //通知的唯一标识,在一个应用程序中不同的通知要区别开来
 23     private static final int NO1 = 0x1001;
 24     private static final int NO2 = 0x1002;
 25     private static final int NO3 = 0x1003;
 26     private static final int NO4 = 0x1004;
 27     //进度条要用
 28     private int progress = 1;
 29 
 30     @Override
 31     protected void onCreate(Bundle savedInstanceState) {
 32         super.onCreate(savedInstanceState);
 33         setContentView(R.layout.activity_main);
 34     }
 35 
 36     //click方法,和xml文件中的各个按钮的onClick属性的值要一致
 37     public void click(View view) {
 38         //创建NotificationManager
 39         final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
 40         //用switch语句控制四个控件
 41         switch (view.getId()) {
 42         case R.id.btn_01: {
 43             Notification notification = new Notification();
 44             notification.icon = R.drawable.ic_launcher;
 45             notification.tickerText = "有消息了。。。";
 46             Intent intent = new Intent(this, MainActivity.class);
 47             PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
 48                     intent, PendingIntent.FLAG_UPDATE_CURRENT);
 49             notification.setLatestEventInfo(this, "3.0以前的通知", "试试而已", pendingIntent);
 50             notification.when = System.currentTimeMillis();
 51             notification.defaults = Notification.DEFAULT_ALL;
 52             notification.flags = Notification.FLAG_AUTO_CANCEL;
 53             notification.number = 1;
 54             notification.vibrate = new long[]{0, 4000};
 55             manager.notify(NO1, notification);
 56         }
 57         break;
 58         case R.id.btn_02:{
 59             //大视图文本通知
 60             //创建消息构造器,在扩展包
 61             NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
 62             //设置当有消息是的提示,图标和提示文字
 63             builder.setSmallIcon(R.drawable.ic_launcher).setTicker("有新消息了");
 64             //需要样式
 65             BigTextStyle style = new BigTextStyle();
 66             style.setBigContentTitle("上课通知");//通知的标题
 67             style.bigText("今天下午要在综B303上jsp");//通知的文本内容
 68             //大视图文本具体内容
 69             style.setSummaryText("这是正常的课程安排,请各位同学按时上课");
 70             builder.setStyle(style);
 71             //显示消息到达的时间,这里设置当前时间
 72             builder.setWhen(System.currentTimeMillis());
 73             //获取一个通知对象
 74             Notification notification = builder.build();
 75             notification.flags = Notification.FLAG_AUTO_CANCEL;
 76             //发送(显示)通知
 77             //notify()第一个参数id An identifier for this notification unique within your application
 78             //get?意思说,这个通知在你的应用程序中唯一的标识符
 79             manager.notify(NO2, notification);
 80         }
 81         break;
 82         
 83         case R.id.btn_03:{
 84             //大视图图片通知
 85             NotificationCompat.Builder builderPic = new Builder(this);
 86             builderPic.setSmallIcon(R.drawable.ic_launcher).setTicker("新浪体育提醒");
 87             //进行设置
 88             BigPictureStyle pictureStyle = new BigPictureStyle();
 89             pictureStyle.setBigContentTitle("新浪体育 快船VS骑士 ");
 90             pictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_game));
 91             pictureStyle.setSummaryText(" 快船VS骑士 天王山之战!!!");//不要在意文字
 92             //设置样式
 93             builderPic.setStyle(pictureStyle);
 94             //设置显示的时间
 95             builderPic.setWhen(System.currentTimeMillis());
 96             Notification notification = pictureStyle.build();
 97             notification.flags = Notification.FLAG_AUTO_CANCEL;
 98             //
 99             manager.notify(NO3, notification);
100         }
101         break;
102         
103         case R.id.btn_04:{
104             //进度条通知
105             final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(this);
106             builderProgress.setSmallIcon(R.drawable.ic_launcher).setTicker("进度条通知");
107             builderProgress.setProgress(100, progress, false);
108             final Notification notification = builderProgress.build();
109             //发送一个通知
110             manager.notify(NO4, notification);
111             //创建一个计时器
112             Timer timer = new Timer();
113             timer.schedule(new TimerTask(){
114 
115                 @Override
116                 public void run() {
117                     Log.i("progress",progress+"");
118                     while(progress <= 100){
119                         progress ++;
120                         try {
121                             Thread.sleep(300);
122                         } catch (InterruptedException e) {
123                             // TODO Auto-generated catch block
124                             e.printStackTrace();
125                         }
126                         //更新进度条
127                         builderProgress.setProgress(100, progress, false);
128                         //再次通知
129                         manager.notify(NO4, builderProgress.build());
130                     }
131                     //计时器退出
132                     this.cancel();
133                     //进度条退出
134                     manager.cancel(NO4);
135                     return;//结束方法
136                 }
137                 
138             }, 0);
139         }
140         break;
141         
142         default:
143             break;
144         }
145     }
146 
147 }
View Code

   5、运行:我的虚拟机版本是4.0的,按住通知左(右)滑动就可以让通知小时了。效果如下:

android的几种“通知”方式简单实现(Notification&amp;NotificationManager)第6张

免责声明:文章转载自《android的几种“通知”方式简单实现(Notification&amp;amp;NotificationManager)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇小米3怎么开通上海地铁刷卡功能latex建立参考文献的超链接下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Kafka:生产者

Kafka java客户端数据生产流程解析 ProducerRecord ProducerRecord 含义: 发送给Kafka Broker的key/value 值对 //ProducerRecord的成员变量 public class ProducerRecord<K, V> { private final String top...

转 Android 4.0后,自定义Title报错 You cannot combine custom titles with other title feature

自定义Titlebar时为了避免冲突 需要修改:AndroidManifest.xml android:theme="@style/mystyle" styles.xml文件中需要加上下面内容 <style name="mystyle" parent="android:Theme"> <item name="android:windo...

appium 移动端自动化测试工具(4)

一 简介 原生应用和web应用 (1)原生应用是特别为某种操作系统开发的. (2)Web应用本质上是为移动浏览器设计的基于Web的应用. (3)混合应用大家都知道是原生应用和Web应用的结合体,採用了原生应用的一部分、Web应用的一部分,所以必须部分在设备上执行、部分在Web上执行。 appium架构 二 appium特点 三 工作原理 ap...

php大文件上传(切片)方法

1、介绍enctype enctype属性规定发送到服务器之前应该如何对表单数据进行编码。 enctype作用是告知服务器请求正文的MIME类型(请求消息头content-type的作用一样) 1、1 enctype的取值有三种 值 描述 application/x-www-form-urlencoded 在发送前编码所有字符(默认) mult...

Storm实战:在云上搭建大规模实时数据流处理系统(Storm+Kafka)

在大数据时代,数据规模变得越来越大。由于数据的增长速度和非结构化的特性,常用的软硬件工具已无法在用户可容忍的时间内对数据进行采集、管理和处理。本文主要介绍如何在阿里云上使用Kafka和Storm搭建大规模消息分发和实时数据流处理系统,以及这个过程中主要遭遇的一些挑战。实践主要立足建立一套汽车状态实时监控系统,可以在阿里云上立即进行部署。   实时大数据处理...

[修复] 启动期间超频失败的错误信息

  一些 PC 用户报告说,他们在尝试启动 PC 时会看到“超频失败”错误。根据主板制造商的不同,错误信息略有不同。在大多数情况下,受影响的用户表示,尽管出现了此消息,但他们从未超频过默认频率。 引导期间超频失败错误消息 在大多数情况下,“超频失败”错误的发生是由于一个小故障,最终欺骗了一系列启动过程,即您的系统实际上是在以默认频率运行时超频的。在这种情况...