android通知-Notification
android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习。
1.新建一个项目,在layout布局里写两个按钮,一个用来开启通知,一个用来关闭通知。下面直接上布局代码。
<RelativeLayout 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:gravity="center"
> <Button
android:id="@+id/bt_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="openNotify"
android:text="open" />
<Button
android:id="@+id/bt_down"
android:layout_below="@id/bt_up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="closeNotify"
android:text="close" /> </RelativeLayout>
然后就是代码的实现了,还是直接上代码,很简单,相信大家一看就明白。
package com.example.demo; 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.view.View;
/**
*
* @author jianww
* 2015-12-16
*
*/
public class MainActivity extends Activity { /**
* 1.先建立通知管理器,得到通知服务
* 2.创建通知对象,发出一个通知。
* 3.
*/
//1.建立通知管理器,
private NotificationManager notifyManager;
//2.声明通知对象变量。
private Notification notify;
//3.创建意图,当点击通知时,打开相应的意思对象,跳转到对应的类。
private Intent intent;
/*
* Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast ,
getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,
而是在外部执行 pendingintent时,调用intent的。正由于pendingintent中 保存有当前
App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里
的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。
另外还可以处理intent执行后的操作。常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,
而Pendingintent,一般用在 Notification上,可以理解为延迟执行的intent,
PendingIntent是对Intent一个包装。本例用pendingIntent可以从通知中打开要打开的app中的对象。
*/
private PendingIntent pendIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
//打开通知
public void openNotify(View v) {
//创建通知对象实例,传入默认的通知对象图片,通知标题,通知发出时间。
notify = new Notification(R.drawable.ic_launcher,"通知",System.currentTimeMillis());
//创建意图。此意图不会立刻执行,只有当pendingIntent执行时,才会执行传入里面的意图。
intent = new Intent(getApplicationContext(),MainActivity.class);
//得到pendintent对象实例。设置此延时意图的标记。
pendIntent = PendingIntent.getActivity(getApplicationContext(), 100, intent, 0);
//设置通知的标题与内容。
notify.setLatestEventInfo(getApplicationContext(), "通知", "通知的内容", pendIntent);
//设置通知的标记为默认。
notify.flags = Notification.FLAG_AUTO_CANCEL;
//开始通知。
notifyManager.notify(100, notify);
}
//关闭通知。
public void closeNotify(View v) {
//关闭通知。
pendIntent.cancel();
} }
就是这么简单,只是用来复习一下基础知识。^_^
android通知-Notification的更多相关文章
- Android 通知(Notification)
1.介绍 2.常用属性 3.java后台代码 package com.lucky.test30notification; import android.app.Notification; import ...
- Android通知Notification全面剖析
通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通知栏查看通知的详细信息. 通知区域和抽屉式通知栏均是由系统控 ...
- Android——通知 Notification
链接:http://jingyan.baidu.com/article/77b8dc7fde875a6175eab641.html http://www.2cto.com/kf/201502/3749 ...
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。
常用的程序通知,显示到主页面的顶部栏. package com.lixu.tongzhi; import android.app.Activity; import android.app.Notifi ...
- Android 状态栏通知Notification、NotificationManager简介
Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...
- Android的状态栏通知(Notification)
通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...
- Android简易实战教程--第三十八话《自定义通知NotifiCation》
上一篇小案例,完成了一个普通的通知,点击通知启动了一个活动.但是那里的通知没有加入些"靓点",这一篇就给它加入自定义的布局,完成自定义的通知. 应用:比如QQ音乐为例,当点击音乐播 ...
- Android开发——Notification通知的各种Style详解
本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...
随机推荐
- Eclipse创建maven的Web项目
MAVEN作用:管理jar包 1.首先新建一个maven项目,看图: 2.按照以上步骤就可以创建一个maven项目,可以看到最下图的目录结构,但是这样的目录结构是不对的,需要做一些修改. 首先为了避免 ...
- transform scale 背景图片模糊怎么办?
transform: translateZ(0) scale(1, 1); 就是这样(摊手表情),不晓得什么原理.
- 误删ibdata1文件恢复方法
注意:以下演示过程前提为mysqld进程仍在运行中,否则无法使用下面演示过程进行恢复! 1.手工制造故障,删除ibdata1文件,注意不要重启mysql shell > rm -rf ibdat ...
- (转) CCTextFieldTTF输入框
CCTextFieldTTF输入框 分类: cocos2d-x 2013-04-08 16:32 2964人阅读 评论(1) 收藏 举报 新建工程,testInput 修改HelloWorldScen ...
- python(八)内置模块logging/os/time/sys/json/pickle
模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少 ...
- winform基础,主要控件简单介绍,以及小练习
WinForm - C/S B/S 客户端应用程序 - 是需要安装在用户电脑上才可以使用的程序特点:不需要联网也可以打开使用部分功能但是现在的情况是许多功能依然需要互联网的支持 代码部分在用户电脑上执 ...
- storm配置项目
java.library.path: “/usr/local/lib:/opt/local/lib:/usr/lib”### storm.* configs are general configura ...
- [问题2014A04] 复旦高等代数 I(14级)每周一题(第六教学周)
[问题2014A04] 设 \(A,B,C,D\) 均为 \(n\) 阶方阵. (1) 若 \(A^2=A\), \(B^2=B\), \((A+B)^2=A+B\), 证明: \(AB=BA=0\ ...
- abort终止正在进行中的的ajax请求
核心:调用XMLHttpRequest对象上的abort方法 jQuery的ajax方法有自己的超时时间设置参数: $.ajax({type:'POST', url:'b.php', data:' ...
- Redis哈希表的实现要点
Redis哈希表的实现要点 哈希算法的选择 针对不同的key使用不同的hash算法,如对整型.字符串以及大小写敏感的字符串分别使用不同的hash算法: 整型的Hash算法使用的是Thomas Wang ...