android笔记:Notification通知的使用
通知(Notification),当某个应用程序希望向用户发出一些提示信息,而该应用程序又不在前台运行时,就可以借助通知来实现。
发出一条通知后,手机最上方的状态栏中会显示一个通知的图标,下拉状态栏后可以看到通知的详细内容。
《第一行代码》中的Notification的构造方法、setLatestEventInfo等方法已经过时了,但是思想还是一致的。
最新的具体做法如下:
1.需要一个 NotificationManager 来对通知进行管理,可以调用Context 的getSystemService(NOTIFICATION_SERVICE)方法获取到;
2.创建一个 Notification 对象,可通过NotificationCompat的Builder方法来实现.
setTicker()用来显示通知在状态栏的提示信息,
setSmallIcon()指定通知的图标
setContentTitle指定通知的标题,setContentText指定通知内容
setContentIntent指定跳转界面的PendingIntent,PendingIntent 简单地理解为延迟执行的 Intent
3. notify()方法就可以让通知显示出来了。
notify()方法接收两个参数,第一个参数是 id,要保证为每个通知所指定的 id 都是
不同的。第二个参数则是 Notification 对象,这里直接将我们刚刚创建好的 Notification 对象
传入即可。
具体代码如下:
MainActivity:
package com.example.notificationtest; import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button sendNotice; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sendNotice = (Button) findViewById(R.id.send_notice);
sendNotice.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.send_notice:
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent intent = new Intent(this, NotificationActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("This is ticker text")
.setContentTitle("This is title")
.setAutoCancel(true)
.setContentText("This is content text")
.setContentIntent(pi)
.build();
manager.notify(1, notification);
break;
default:
break;
}
} }
NotificationActivity:
package com.example.notificationtest; import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle; public class NotificationActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.notification_layout);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(1);
} }
activity_main.xml:
<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:orientation="vertical" > <Button
android:id="@+id/send_notice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send notice"
/> </LinearLayout>
notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:text="This is notification layout"
/> </RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.notificationtest"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.notificationtest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NotificationActivity" >
</activity> </application> </manifest>
android笔记:Notification通知的使用的更多相关文章
- Android开发——Notification通知的各种Style详解
本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...
- Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API
想要看全部设置的请看这一篇 [转]NotificationCopat.Builder全部设置 常用设置: 设置属性 说明 setAutoCancel(boolean autocancel) 设置点击信 ...
- android之Notification通知
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. pac ...
- Android笔记:通知
可以在活动里创建,也可以在广播接收器里创建,还可以在服务里创建. NotificationManager manager = (NotificationManager)getSystemService ...
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- Android Notification通知详解
根据activity的生命周期,在activity不显示时,会执行onStop函数(比如按下home键),所以你在onStop函数(按退出键除外)里面把notification放在通知栏里,再此显示时 ...
- Android Notification通知详细解释
Android Notification通知具体解释 Notification: (一).简单介绍: 显示在手机状态栏的通知. Notification所代表的是一种具有全局效果的通 ...
- Android的状态栏通知(Notification)
通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...
- 【Android】状态栏通知Notification、NotificationManager详解(转)
在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类: NotificationMa ...
随机推荐
- [转载]QQ空间技术架构之深刻揭密
1. 拥有5.5亿的活跃用户 2. 过万台的设备 3. 数千万级别的同时在线 4. 数十亿级别的全站PV 5. P级的UGC存储量 6. 每天千亿级别的服务请求 图1--QQ空间海量服务数据规模 接下 ...
- 合并excel-MergeExcel
MergeExcel 将需要合并的excel放入该目录下,包含xls和xlsx 点击运行按钮: 输入包含多少列: 等待运行,完毕! 基于python编写,源码和exe于 TTyb
- excel表格中关于 撤销工作表保护密码
利用宏处理,代码如下: Sub PasswordBreaker() Dim i As Integer, j As Integer, k As Integer Dim l As Integer, m A ...
- 【python】类中__slots__使用
__slots__作用:限制类的属性,只给实例绑定任何属性和方法 如果我们想要限制class的属性怎么办?比如,只允许对Student实例添加name和age属性. 为了达到限制的目的,Python允 ...
- oracle11g安装和基本的使用-转载
一.测试操作系统和硬件环境是否符合,我使用的是win2008企业版.下面的都是step by step看图就ok了,不再详细解释. 请留意下面的总的设置步骤:--------------------- ...
- php操作文件及下载图片脚本
<?php set_time_limit(0); $handle = fopen('article.txt','r'); for($i=0;$i<1;$i++) { $count = 0; ...
- 计算机视觉:关于视觉算法源码中常出现的imageLib库的使用指南
1.ReadImage(CImage &im, char* path)/ WriteImage(CImage &im, char* path) (1)将im强制转换为CByteImag ...
- eclipse中如何关闭运行时自动保存?
Eclipse没有提供自动保存的功能,只能自己写脚本每隔多久保存一次,或监听修改即保存.设置方法:1.打开:preferences>run/debug>launching2.选择save ...
- 剑指Offer:面试题25——二叉树中和为某一值的路径(java实现)
问题描述: 输入一棵二叉树和一个整数,打印出二叉树中结点指的和为输入整数的所有路径.从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.二叉树结点的定义如下: public class Tree ...
- Android dispatchTouchEvent介绍
一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN->ACTION_MOVE->ACTION_MOVE->ACTION_MOVE...->ACTION_ ...