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();
} }

  就是这么简单,只是用来复习一下基础知识。^_^

最新文章

  1. 微信接口php
  2. GTAC 2015将于11月10号和11号召开
  3. 5 Ways to Learn and Remember Absolutely Anything
  4. JSP特点
  5. [RxJS] Reactive Programming - Clear data while loading with RxJS startWith()
  6. AngularJs 基础(60分钟入门) (转)
  7. codeigniter 该脚本在运行300s超时退
  8. (一个)AngularJS获取贴纸Hello World
  9. 一天搞定CSS:表单(form)--20
  10. epclise设置tomcat方法(步骤)(菜鸟巧记二)
  11. Gradle 1.12 翻译——第十八章. 日志
  12. P5283 [十二省联考2019]异或粽子
  13. Tips_关闭按钮的简单实现 &amp;&amp; Felx实现水平垂直居中
  14. MMIO和PIO
  15. SPOJ DQUERY D-query(主席树 区间不同数个数)
  16. 解决Safari页面缓存的问题
  17. IP地址分类以及子网划分
  18. Python3中map函数的问题
  19. IFNULL函数
  20. Spark中RDD转换成DataFrame的两种方式(分别用Java和Scala实现)

热门文章

  1. 处理mysql上百万条数据的优化
  2. httpclient调用https
  3. SNMP OID列表 监控需要用到的OID
  4. centos7 firewall 防火墙 命令
  5. [Mysql] mysql临时表corrupt
  6. 关于Python中输出中文的一点疑问
  7. 从欧几里得距离、向量、皮尔逊系数到http://guessthecorrelation.com/
  8. 利用反射和ResultSetMetaData实现DBUtils的基本功能
  9. Delphi 使用之dll文件生成与调用
  10. Skyfree的毕业论文 《系统封装与部署的深入研究》