常用的程序通知,显示到主页面的顶部栏。

 package com.lixu.tongzhi;

 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;
import android.widget.RemoteViews;
import android.widget.Toast; public class MainActivity extends Activity { private static final int ID = 1987;
private static final int REQUEST_CODE = 123; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn1 = (Button) findViewById(R.id.fasong);
Button btn2 = (Button) findViewById(R.id.qingchu); btn1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
send(); Toast.makeText(getApplicationContext(), "发送通知成功!", 0).show(); }
}); btn2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) { delete(); Toast.makeText(getApplicationContext(), "清除通知成功!", 0).show(); }
}); } private void send() {
// 获取通知管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
// 设置通知栏图片
mBuilder.setSmallIcon(R.drawable.sdfdf); Notification notification = mBuilder.build();
// RemoteViews中自定义Notification布局 RemoteViews cv = new RemoteViews(getApplicationContext().getPackageName(), R.layout.list);
// 设置下拉后通知栏图片
cv.setImageViewResource(R.id.image, R.drawable.meimei);
// 设置内容
cv.setTextViewText(R.id.tv1, "我是小超人");
cv.setTextViewText(R.id.tv2, "我是小超人,主人有什么事情要吩咐。");
notification.contentView = cv; // 设置通知声音或者震动或闪光。
notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
// 通知时间
notification.when = System.currentTimeMillis(); // 需要注意的是,作为选项,此处可以设置MainActivity的启动模式为singleTop,避免重复新建onCreate()。
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
// 当用户点击通知栏的Notification时候,切换回MainActivity。
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), REQUEST_CODE, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.contentIntent = pi; // 通过特定id来发送这个通知
manager.notify(ID, notification); } private void delete() {
// 获取通知管理器
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(ID); } }

需要注意的是,默认Android的Activity为标准模式,即每次都new一个新的Activity出来,不是原先的Activity,在本例中,可以观察到MainActivity中的onCreate()如果不修改启动模式,则每次本调用每次TextView显示的时间不同(递增),所有为了使用原来的Activity、避免重复new一个新的出来,需要:

在AndroidManifest.xml中修改MainActivity启动模式为:singleTop

xml文件:

 <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" > <Button
android:id="@+id/fasong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/qingchu"
android:layout_alignParentTop="true"
android:layout_marginTop="56dp"
android:text="发送通知" /> <Button
android:id="@+id/qingchu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fasong"
android:layout_centerHorizontal="true"
android:layout_marginTop="88dp"
android:text="清除通知" /> </RelativeLayout>
 <?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" > <ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" /> <TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:background="#ff0000" /> <TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv1"
android:layout_toRightOf="@id/image"
android:background="#00ff00" /> </RelativeLayout>

运行效果图:

Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。的更多相关文章

  1. Android通知Notification全面剖析

    通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通知栏查看通知的详细信息. 通知区域和抽屉式通知栏均是由系统控 ...

  2. 在 Xamarin.Android 中使用 Notification.Builder 构建通知

    0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...

  3. Android学习总结(十五) ———— Notification(状态栏通知)基本用法

    一.Notification基本概念  Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...

  4. 【Android】状态栏通知Notification、NotificationManager详解(转)

    在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类:  NotificationMa ...

  5. Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)

    示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...

  6. android通知-Notification

    android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...

  7. Android中的通知—Notification 自定义通知

    Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...

  8. Android 状态栏通知Notification、NotificationManager简介

    Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...

  9. Android的状态栏通知(Notification)

    通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...

随机推荐

  1. Redis学习记录之————微博项目

    Key设计 全局相关的key: 表名 global 列名 操作 备注 Global:userid incr 产生全局的userid Global:postid Incr 产生全局的postid 用户相 ...

  2. Nginx模块学习之————accesskey权限模块使用(简单的m3u8防盗链)

    配置文件:http://www.cnblogs.com/tinywan/p/5983694.html 通过加密后的文件: 正确地址:curl -i http://访问的IP地址(这里是直播节点IP地址 ...

  3. Linux 下操作GPIO(两种方法,驱动和mmap)(转载)

    目前我所知道的在Linux下操作GPIO有两种方法: 1.编写驱动,这当然要熟悉Linux下驱动的编写方法和技巧,在驱动里可以使用ioremap函数获得GPIO物理基地址指针,然后使用这个指针根据io ...

  4. MonkeyRunner学习(3)脚本编辑

    除了cmd直接操作手机,也可以编辑好脚本后,运行脚本,一次操作多个脚本命令 a) 新建py格式脚本,如iReader.py b) 编辑脚本 #导入模块 from com.android.monkeyr ...

  5. XMLHttpRequest对象进行Ajax操作

    XMLHttpRequest 对象的三个常用的属性: 1. onreadystatechange 属性  onreadystatechange 属性存有处理服务器响应的函数. 请求状态改变的事件触发器 ...

  6. 使用fragment兼容低版本的写法

      [1]定义fragment继承V4包中的Fragment    [2]定义的activity要继承v4包中的FragmentActivity   [3]通过这个方法getSupportFragme ...

  7. XML HTML

    XML和HTML常用转义字符 XML和HTML中都有一些特殊的字符,这些字符在XML和HTML中是不能直接使用的,如果必须使用这些字符,应该使用其对应的转义字符. XML常用转义字符: 字符 转义字符 ...

  8. PHP SPL标准库之SplFixedArray使用实例

    SplFixedArray主要是处理数组相关的主要功能,与普通php array不同的是,它是固定长度的,且以数字为键名的数组,优势就是比普通的数组处理更快. 看看我本机的Benchmark测试: i ...

  9. mustache.js渲染带事件的模板

    http://zccst.iteye.com/blog/2183111 最近遇到这样一个问题,预览内容可点击,问题是通过$.Mustache.render("templateId" ...

  10. 探索 Pexpect,第 1 部分:剖析 Pexpect

    Pexpect 是一个用来启动子程序并对其进行自动控制的 Python 模块. Pexpect 可以用来和像 ssh.ftp.passwd.telnet 等命令行程序进行自动交互.本文章介绍 Pexp ...