关于通知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

具体代码

<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>

  4、MainActivity中的代码:

 package com.xin.day_12_notificationdemo1;

 import java.util.Timer;
import java.util.TimerTask; import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.BigPictureStyle;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;
import android.view.View; public class MainActivity extends Activity { //通知的唯一标识,在一个应用程序中不同的通知要区别开来
private static final int NO1 = 0x1001;
private static final int NO2 = 0x1002;
private static final int NO3 = 0x1003;
private static final int NO4 = 0x1004;
//进度条要用
private int progress = 1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} //click方法,和xml文件中的各个按钮的onClick属性的值要一致
public void click(View view) {
//创建NotificationManager
final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//用switch语句控制四个控件
switch (view.getId()) {
case R.id.btn_01: {
Notification notification = new Notification();
notification.icon = R.drawable.ic_launcher;
notification.tickerText = "有消息了。。。";
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, "3.0以前的通知", "试试而已", pendingIntent);
notification.when = System.currentTimeMillis();
notification.defaults = Notification.DEFAULT_ALL;
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.number = 1;
notification.vibrate = new long[]{0, 4000};
manager.notify(NO1, notification);
}
break;
case R.id.btn_02:{
//大视图文本通知
//创建消息构造器,在扩展包
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//设置当有消息是的提示,图标和提示文字
builder.setSmallIcon(R.drawable.ic_launcher).setTicker("有新消息了");
//需要样式
BigTextStyle style = new BigTextStyle();
style.setBigContentTitle("上课通知");//通知的标题
style.bigText("今天下午要在综B303上jsp");//通知的文本内容
//大视图文本具体内容
style.setSummaryText("这是正常的课程安排,请各位同学按时上课");
builder.setStyle(style);
//显示消息到达的时间,这里设置当前时间
builder.setWhen(System.currentTimeMillis());
//获取一个通知对象
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
//发送(显示)通知
//notify()第一个参数id An identifier for this notification unique within your application
//get?意思说,这个通知在你的应用程序中唯一的标识符
manager.notify(NO2, notification);
}
break; case R.id.btn_03:{
//大视图图片通知
NotificationCompat.Builder builderPic = new Builder(this);
builderPic.setSmallIcon(R.drawable.ic_launcher).setTicker("新浪体育提醒");
//进行设置
BigPictureStyle pictureStyle = new BigPictureStyle();
pictureStyle.setBigContentTitle("新浪体育 快船VS骑士 ");
pictureStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.drawable.ic_game));
pictureStyle.setSummaryText(" 快船VS骑士 天王山之战!!!");//不要在意文字
//设置样式
builderPic.setStyle(pictureStyle);
//设置显示的时间
builderPic.setWhen(System.currentTimeMillis());
Notification notification = pictureStyle.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
//
manager.notify(NO3, notification);
}
break; case R.id.btn_04:{
//进度条通知
final NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(this);
builderProgress.setSmallIcon(R.drawable.ic_launcher).setTicker("进度条通知");
builderProgress.setProgress(100, progress, false);
final Notification notification = builderProgress.build();
//发送一个通知
manager.notify(NO4, notification);
//创建一个计时器
Timer timer = new Timer();
timer.schedule(new TimerTask(){ @Override
public void run() {
Log.i("progress",progress+"");
while(progress <= 100){
progress ++;
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//更新进度条
builderProgress.setProgress(100, progress, false);
//再次通知
manager.notify(NO4, builderProgress.build());
}
//计时器退出
this.cancel();
//进度条退出
manager.cancel(NO4);
return;//结束方法
} }, 0);
}
break; default:
break;
}
} }

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

android的几种“通知”方式简单实现(Notification&NotificationManager)的更多相关文章

  1. Android中几种定位 方式

    介绍的几种定位方式 http://www.cnblogs.com/cuihongyu3503319/p/3863867.html 百度地图api: http://lbsyun.baidu.com/in ...

  2. Android学习笔记(36):Android的两种事件处理方式

    Android提供了两种事件处理的方式:基于回调的事件处理 和 基于监听的事件处理. 我们来说的easy理解一点: (1)基于回调的事件处理就是继承GUI组件,并重写该组件的事件处理方法.除了一些特定 ...

  3. android service两种启动方式

    android service的启动方式有以下两种: 1.Context.startService()方式启动,生命周期如下所示,启动时,startService->onCreate()-> ...

  4. Android的四种储存方式(SQLite、FileSystem、SDCardSystem、SharedPreferences)

    主要记录一下安卓中几种常用的存储方式的用法. 一.SQLite 1.创建SQLiteOpenHelper对象(当然SQLiteOpenHelper是抽象类,不能直接创建): 2.通过上面创建的对象调用 ...

  5. android开发两种退出程序方式(killProcess,System.exit)

    KillProcess: 在android中我们如果想要程序的进程结束可以这样写: android.os.Process.killProcess(android.os.Process.myPid()) ...

  6. Android 文件下载三种基本方式

    一.自己封装URLConnection 连接请求类 public void downloadFile1() { try{ //下载路径,如果路径无效了,可换成你的下载路径 String url = & ...

  7. android:Activity四种启动模式简单介绍

    Activity启动模式 能够依据实际的需求为Activity设置相应的启动模式,从而能够避免创建大量反复的Activity等问题 Activity有四种载入模式 1.standard(默认启动模式, ...

  8. Android进程通信之一:两种序列化方式

    2月下旬辞职了,去海南度假到现在,领略了一把三亚风情也算任性和 然而这样任性带来的后果就是..不行了我必须吐槽一句.. 没毕业的找工作就这么难嘛!投了57家一家面试机会都没有,好歹给个面试机会啊!!本 ...

  9. Android平台中实现对XML的三种解析方式

    本文介绍在Android平台中实现对XML的三种解析方式. XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能. 在 ...

随机推荐

  1. Mybatis中的update动态SQL语句

    Mybatis中的CRUD操作(增删改查)中,简单的SQL操作比较直观,如查找操作: <select id="findBySrcId" resultMap="ent ...

  2. ZOJ2417 Lowest Bit 2017-04-18 20:53 38人阅读 评论(0) 收藏

    Lowest Bit Time Limit: 2 Seconds      Memory Limit: 65536 KB Given an positive integer A (1 <= A ...

  3. Hdu428 漫步校园 2017-01-18 17:43 88人阅读 评论(0) 收藏

    漫步校园 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  4. console的所有用法

    http://jingyan.baidu.com/article/e75aca855c6419142edac6c1.html  参考它. console.info(), console.debug() ...

  5. 提高Android和iOS调试编译速度

    http://www.cnblogs.com/findumars/p/7841252.html 提高Android和iOS调试编译速度   如果您使用Delphi开发App,就会遇到:Android和 ...

  6. Android 了解1G 2G 3G 知识

    了解1G 2G 3G 相关知识,对网络通讯制式进行了解即可 1.这种网络通讯制式是一步一步发展起来的,由最开始的1G(最典型的手机,例如:大哥大,1G这种制式只能语音通话). 2.后来出现的2G,2G ...

  7. SSD测试第一神器——FIO

    原文 地址 http://www.ssdfans.com/ssd%E6%B5%8B%E8%AF%95%E7%AC%AC%E4%B8%80%E7%A5%9E%E5%99%A8-fio-2/ 对于SSD性 ...

  8. BitAdminCore框架应用篇:(四)核心套件querySuite按钮功能

    索引 NET Core应用框架之BitAdminCore框架应用篇系列 框架演示:http://bit.bitdao.cn 框架源码:https://github.com/chenyinxin/coo ...

  9. 《ASP.NET MVC 5 破境之道》:第一境 ASP.Net MVC5项目初探 — 第三节:View层简单改造

    第一境 ASP.Net MVC5项目初探 — 第三节:View层简单改造 MVC默认模板的视觉设计从MVC1到MVC3都没有改变,比较陈旧了:在MVC4中做了升级,好看些,在不同的分辨率下,也能工作得 ...

  10. With语句上下文管理

    在平时工作中总会有这样的任务,它们需要开始前做准备,然后做任务,然后收尾清理....比如读取文件,需要先打开,读取,关闭 这个时候就可以使用with简化代码,很方便 1.没有用with语句 1 2 3 ...