在一个Activity中点击按钮,产生一个通知栏消息通知。

package cn.luxh.mynotice;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent; public class MainActivity extends Activity {
private static String TAG = "MyNotice";
private static int notificationID = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handleBtnDisplayNotification();
} /**
* 点击按钮,创建消息通知
*/
private void handleBtnDisplayNotification() {
Button btnDisplayNotification = (Button) findViewById(R.id.btn_display_notice);
btnDisplayNotification.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i(TAG, "点击按钮");
Intent i = new Intent(MainActivity.this,NoticeViewActivity.class);
i.putExtra("notificationID", notificationID);
PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, i, 0);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); //创建通知对象
Notification n = new Notification.Builder(MainActivity.this)
.setContentTitle("有来自黑莓的新邮件")
.setContentText("黑莓,依然经典!")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pi)
.build();
//振动手机
n.vibrate = new long[]{100,250,100,500};
nm.notify(notificationID, n);
}
});
} }
package cn.luxh.mynotice;

import android.app.Activity;
import android.app.NotificationManager;
import android.os.Bundle;
import android.util.Log; /**
* 在通知栏点击通知后显示消息的界面
* @author Luxh
*
*/
public class NoticeViewActivity extends Activity{ private static String TAG = "MyNotice"; @Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "NoticeViewActivity onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notice_view); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//取消通知
nm.cancel(getIntent().getExtras().getInt("notificationID"));
}
}

运行效果:

Android--使用Notification在通知栏显示消息的更多相关文章

  1. Android学习笔记通过Toast显示消息提示框

    显示消息提示框的步骤 这个很简单我就直接上代码了: Button show = (Button)findViewById(R.id.show); show.setOnClickListener(new ...

  2. Android 主页面顶部栏的通知Notification ,可以自定义通知消息栏的风格,并且点击通知栏进人本程序。

    常用的程序通知,显示到主页面的顶部栏. package com.lixu.tongzhi; import android.app.Activity; import android.app.Notifi ...

  3. 通知栏发送消息Notification(可以使用自定义的布局)

    一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见.代码在此时机发送一个Notifica ...

  4. android自定义Notification通知栏实例

    项目有个需求,需要在发送Notification的时候动态给定url的图片.大概思路如下:自己定义一个Notification的布局文件,这样能够很方便设置View的属性. 首先加载网络图片,使用Bi ...

  5. 转 android客户端版本检测更新,服务下载,通知栏显示

    看图, 只要点击取消或是下载完毕 通知才会消失!      代码是大部分是借用别人的,再自己修改,达到自己所需要的效果 xml文件 update.xml <?xml version=" ...

  6. Android开发——Notification通知的各种Style详解

    本来是想与之前讲解使用Notification通知使用一起写的,查看了资料,觉得有必要将这Style部分单独拿出来讲解 前篇:Android开发——Notification通知的使用及Notifica ...

  7. Android开发——Notification通知的使用及NotificationCopat.Builder常用设置API

    想要看全部设置的请看这一篇 [转]NotificationCopat.Builder全部设置 常用设置: 设置属性 说明 setAutoCancel(boolean autocancel) 设置点击信 ...

  8. 安卓开发_浅谈Notification(通知栏)

    Notification通知栏是显示在手机状态的消息,代表一种全局效果的通知 快速创建一个Notification的步骤简单可以分为以下四步: 第一步:通过getSystemService()方法得到 ...

  9. Android应用实现Push推送消息原理

            本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我 们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅 ...

随机推荐

  1. C++ 拆分字符串-copy()

    c++拆分字符串方法: #include <iostream>#include <string>#include <sstream>#include <alg ...

  2. 从K近邻算法谈到KD树、SIFT+BBF算法

    转自 http://blog.csdn.net/v_july_v/article/details/8203674 ,感谢july的辛勤劳动 前言 前两日,在微博上说:“到今天为止,我至少亏欠了3篇文章 ...

  3. java中 == 与 equal区别 转

    java中的数据类型,可分为两类:1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean   他们之间的比较,应用双等号(==) ...

  4. DIV+CSS命名规范-转载2

    一.CSS文件及样式命名1.CSS文件命名规范 全局样式:global.css: 框架布局:layout.css: 字体样式:font.css: 链接样式:link.css: 打印样式:print.c ...

  5. TextBox(只允许输入字母或者数字)

    实现如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...

  6. python正则表达式的学习记录

    match和findall的区别以及有括号和无括号的区别 strvar = "hello\n\nworld" find_re = re.compile("hello[.| ...

  7. mysql_connect和mysql_pconnect区别(转)

    php中mysql_pconnect()的实现方式:其实mysql_pconnect()本身并没有做太多的处理,它唯一做的只是在php运行结束后不主动close掉mysql的连接.mysql_pcon ...

  8. PLSQL_基础系列10_子查询WITH AS(案例)

    2015-05-29 Created By BaoXinjian

  9. DBA_Oracle冷备份案例脚本本法(案例)

    2014-08-10 Created By BaoXinjian

  10. BestCoder Round #85 hdu5776 sum

    sum 题意: 问题描述 给定一个数列,求是否存在连续子列和为m的倍数,存在输出YES,否则输出NO 输入描述 输入文件的第一行有一个正整数T,表示数据组数. 接下去有T组数据,每组数据的第一行有两个 ...