Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。
常用的程序通知,显示到主页面的顶部栏。
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 ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。的更多相关文章
- Android通知Notification全面剖析
通知 通知是您可以在应用的常规 UI 外部向用户显示的消息.当您告知系统发出通知时,它将先以图标的形式显示在通知区域中.用户可以打开抽屉式通知栏查看通知的详细信息. 通知区域和抽屉式通知栏均是由系统控 ...
- 在 Xamarin.Android 中使用 Notification.Builder 构建通知
0 背景 在 Android 4.0 以后,系统支持一种更先进的 Notification.Builder 类来发送通知.但 Xamarin 文档含糊其辞,多方搜索无果,遂决定自己摸索. 之前的代码: ...
- Android学习总结(十五) ———— Notification(状态栏通知)基本用法
一.Notification基本概念 Notification是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容.我们在用手机的时候 ...
- 【Android】状态栏通知Notification、NotificationManager详解(转)
在Android系统中,发一个状态栏通知还是很方便的.下面我们就来看一下,怎么发送状态栏通知,状态栏通知又有哪些参数可以设置? 首先,发送一个状态栏通知必须用到两个类: NotificationMa ...
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...
- android通知-Notification
android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...
- Android中的通知—Notification 自定义通知
Android中Notification通知的实现步骤: 1.获取NotificationManager对象NotificationManager的三个公共方法:①cancel(int id) 取消以 ...
- Android 状态栏通知Notification、NotificationManager简介
Notification(通知)一般用在电话,短信,邮件,闹钟铃声,在手机的状态栏上就会出现一个小图标,提示用户处理这个通知,这时手从上方滑动状态栏就可以展开并处理这个通知: 在Android系统中, ...
- Android的状态栏通知(Notification)
通知用于在状态栏显示消息,消息到来时以图标方式表示,如下: 如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息. 1.Layout布局文件: <RelativeLayout xmlns:an ...
随机推荐
- sql for xml 嵌套
找很久.原来差一个ELEMENTS 关键字. 想到哪里插入子节点.就直接写一条语句,加一个ELEMENTS. 为什么baidu这么就都找不到.到处都是转来转去的东西.郁闷. select h.*,(s ...
- java获取中问名字的首字母
public class FirstLetterUtil { private static int BEGIN = 45217; private static int END = 63486; // ...
- [转载] YouCompleteMe
原文: http://blog.marchtea.com/archives/161#rd?sukey=fc78a68049a14bb2ba33c15948d34749e1eb616df07efe977 ...
- .net 连接sqlserver类库
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- Linux下的字符集问题
怎么设置Linux系统中文语言,这是很多小伙伴在开始使用Linux的时候,都会遇到一个问题,就是终端输入命令回显的时候中文显示乱码.出现这个情况一般是由于没有安装中文语言包,或者设置的默认语言有问题导 ...
- python中if __name__ == '__main__'
python 中__name__ = '__main__' 的作用,到底干嘛的? 有句话经典的概括了这段代码的意义: “Make a script both importable and execut ...
- Javascript 弱类型:除法结果是小数
由于javascript是弱类型,只有一种var类型,所以在运算时不会自动强制转换,所以计算的结果是多少就是多少,但java中的17/10的结果就是1(强类型与弱类型)比如:console.log(M ...
- opencl初探-sobel检测
sobel检测的C版本,neon和GPU的时间比较. Platform: LG G3, Adreno 330 ,img size 3264x2448 sobel: C code neon GPU 73 ...
- PHP 递归的密码
http://www.cnsecer.com/4146.html 说实话 真的很炫 好像还是不好理解啊
- retrifit
Retrofit 特点 性能最好,处理最快 使用REST API时非常方便: 传输层默认就使用OkHttp: 支持NIO: 拥有出色的API文档和社区支持 速度上比volley更快: 如果你的应用程序 ...