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

 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. FFPEG 转码记录------解决了有流,但是没有码率和FPS?

    命令行:(已经测试成功) ffmpeg -i rtmp://localhost/live/S0000_8 -c:v libx264 -b:v 500k -c:a libfdk_aac -b:a 64k ...

  2. rpm 更新/升级 软件包(libGL-devel手动安装过程)

    rpm参数解释 -i 安装 -h 解压rpm的时候打印50个斜条 (#) -v 显示详细信息 升级命令rpm -Uvh rpm文件名 参数解释 -U 升级 -h 解压rpm的时候打印50个斜条 (#) ...

  3. mac homebrew PHP

    启动PHP p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px "Andale Mono"; color: #29f914; ...

  4. spring.hibernate设置参数的位置问题

    1.spring的注解设置在set方法上 2.hibernate的注解设置在get方法上

  5. [转]c++面向对象基础

    from: here 1. C++面向对象程序设计的重要概念 早期革命影片里有这样一个角色,他说:“我是党代表,我代表党,我就是党.”后来他给同志们带来了灾难. 会用C++的程序员一定懂得面向对象程序 ...

  6. 关于MySQL大牛周振兴的博客

    博客内容比较丰富 MySQL管理 数据恢复 linux TCP 个人生活感触 不过内容总体是笔记式的,更适合自己看,不适合初学者去follow.不过对MySQL比较熟悉的人,可以看看,作为扩展眼界的途 ...

  7. 制作Aspose CHM文档的过程记录

    欢迎和大家交流技术相关问题: 邮箱: jiangxinnju@163.com 博客园地址: http://www.cnblogs.com/jiangxinnju GitHub地址: https://g ...

  8. program testy data

    做项目得用数据吧,拿去.... 1.Data.gov搜索   美国政府去年承诺使所有政府数据都能在网上免费获得.这个网站是第一阶段,作为一个门户网站,囊括了从气候到犯罪的一切惊人的信息.   2. 美 ...

  9. HTML5 localStorage and sessionStorage

    转载至:http://blog.csdn.net/fdipzone/article/details/25517615 HTML5 提供两种web存储方法,localStorage 与 sessionS ...

  10. css禁止用户选中文本(转)

    body{ -moz-user-select:none;/*火狐*/ -webkit-user-select:none;/*webkit浏览器*/ -ms-user-select:none;/*IE1 ...