通知栏消息(Notification)初步

Notification是用来在通知中心中显示信息的,这里讲解了其最简单的使用方式。
关于PendingIntent和Intent的区别可以参考这篇文章:http://blog.csdn.net/zeng622peng/article/details/6180190
MainActivity.java
package com.kale.notification; 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;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { static final int NOTIFICATION_ID = 0x123;
NotificationManager nm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取系统服务来初始化对象
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); initView(); } private void initView() {
// TODO 自动生成的方法存根
Button b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
sent(arg0);
}
});
Button b2 = (Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() { @Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
del(arg0);
}
});
} public void sent(View source) {
//设置点击后启动的activity
Intent intent = new Intent(MainActivity.this,MainActivity.class);
//将Intent封装进PendingIntent中,点击通知的消息后,就会启动对应的程序
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); Notification notification = new Notification.Builder(this)
.setAutoCancel(true)//点击通知后,自动消失
.setTicker("在顶部状态栏的提示信息")//在顶部状态栏中的提示信息
.setSmallIcon(R.drawable.ic_launcher)//设置顶部状态栏的小图标
.setContentTitle("这是内容的标题")//设置通知中心的标题
.setContentText("这是通知的内容")//设置通知中心中的内容
.setDefaults(Notification.DEFAULT_SOUND|//设置使用默认的声音
Notification.DEFAULT_LIGHTS|//设置使用默认的LED
Notification.DEFAULT_VIBRATE)//设置使用默认的振动
//.setVibrate(new Long[] {0,50,100,150}) 设置自定义的振动
/*.setSound(Uri.parse("file:///sdcard/click.mp3"))*/
.setWhen(System.currentTimeMillis())
.setContentIntent(pIntent)//该通知要启动的Intent
.build();
//发送该通知
nm.notify(NOTIFICATION_ID,notification);
} public void del(View v) {
//取消通知
nm.cancel(NOTIFICATION_ID);
} }
补充内容(来自:http://www.oschina.net/code/snippet_270292_14489):
NotificationManager常用方法介绍:public void cancelAll() 移除所有通知(只是针对当前Context下的Notification)public void cancel(int id) 移除标记为id的通知 (只是针对当前Context下的所有Notification)public void notify(String tag ,int id, Notification notification) 将通知加入状态栏,标签为tag,标记为idpublic void notify(int id, Notification notification) 将通知加入状态栏,标记为id常量:DEFAULT_ALL 使用所有默认值,比如声音,震动,闪屏等等DEFAULT_LIGHTS 使用默认闪光提示DEFAULT_SOUNDS 使用默认提示声音DEFAULT_VIBRATE 使用默认手机震动 以上的效果常量可以叠加,即通过notification.defaults =DEFAULT_SOUND|DEFAULT_VIBRATE; notification.defaults |= DEFAULT_SOUND <!-- 闪光灯的权限 -->
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<!-- 振动的权限 -->
<uses-permission android:name="android.permission.VIBRATE"/>
布局文件(仅仅就是两个按钮 ⊙﹏⊙b汗)
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.kale.notification.MainActivity" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="18dp"
android:layout_toRightOf="@+id/textView1"
android:text="发送" /> <Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="47dp"
android:text="取消" /> </RelativeLayout>
通知栏消息(Notification)初步的更多相关文章
- Android 之 信息通知栏消息Notification
Notification是安卓手机顶部的消息提示 这里我们分别设置两个按钮,来实现顶部消息的发送和取消 功能实现 首先要在主Activity中设置一个通知控制类 NotificationManager ...
- 通知栏发送消息Notification(可以使用自定义的布局)
一个简单的应用场景:假如用户打开Activity以后,按Home键,此时Activity 进入-> onPause() -> onStop() 不可见.代码在此时机发送一个Notifica ...
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...
- React Native之通知栏消息提示(ios)
React Native之通知栏消息提示(ios) 一,需求分析与概述 详情请查看:React Native之通知栏消息提示(android) 二,极光推送注册与集成 2.1,注册 详情请查看:Rea ...
- React Native之通知栏消息提示(android)
React Native之通知栏消息提示(android) 一,需求分析与概述 1.1,推送作为手机应用的基本功能,是手机应用的重要部分,如果自己实现一套推送系统费时费力,所以大部分的应用都会选择使用 ...
- android实现通知栏消息
一.原理 消息推送有两种,一种是客户端定时直接到服务器搜索消息,如果发现有新的消息,就获取消息下来:另一种是服务器向客户端发送消息,也就是当有信息消息时,服务器端就会向客户端发送消息. 二.步骤(代码 ...
- Android中使用Notification实现进度通知栏(Notification示例三)
我们在使用APP的过程中,软件会偶尔提示我们进行版本更新,我们点击确认更新后,会在通知栏显示下载更新进度(已知长度的进度条)以及安装情况(不确定进度条),这就是我们今天要实现的功能.实现效果如下: 在 ...
- Android中使用Notification实现宽视图通知栏(Notification示例二)
Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...
- Android中使用Notification实现普通通知栏(Notification示例一)
Notification是在你的应用常规界面之外展示的消息.当app让系统发送一个消息的时候,消息首先以图表的形式显示在通知栏.要查看消息的详情需要进入通知抽屉(notificationdrawer) ...
随机推荐
- hdu 5427(排序水题)
排序 年轻的排前面 名字中可能有空格 Sample Input21FancyCoder 19962FancyCoder 1996xyz111 1997 Sample OutputFancyCoderx ...
- CSS工具之CSS重置(CSS Reset)
css代码: /* v1.0 | 20080212 */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, ...
- relay.index' not found
主备搭建时,备库上执行以下语句报错: (root@localhost:mysql.sock)[(none)]>change master to master_host='192.168.0.57 ...
- University Entrace Examination zoj1023
学校招收学生 优先级按照: 分数 是否本地 志愿先后 相当于 女的开后宫 对gs进行略微修改 结束的条件为每个男的表白完所有女的 第二部分比较时 找出女的后宫里的吸引力最弱的男的比较 ...
- maven打包时包含本地jar
项目中需要使用maven的打包工具,生成zip压缩包,使用的插件是assembly-plugin.因为一些特殊的原因,需要使用一些本地的jar进行依赖,加载外部jar后编码过程中没有任何问题,但是打包 ...
- 001.FTP简介及相关文件
一 FTP简介 FTP(File Transfer Protocol)文件传输协议,用于Internet上控制文件的双向传输. 下载:远程主机拷贝文件至本地: 上传:本地主机拷贝文件至远程. 二 FT ...
- 【教程】使用gitee搭建免费的图床
前几天七牛云的免费图床测试域名回收,导致我上传的图片都不能访问!要配置自定义域名,域名还要绑定主机.没有云主机的我开始想你们搞一个免费的图床,并且数据也不会丢失呢 ? 想到之前自己在GitHub上 ...
- hdu 4169 二分匹配最大独立集 ***
题意:有水平N张牌,竖直M张牌,同一方向的牌不会相交.水平的和垂直的可能会相交,求最少踢出去几张牌使剩下的牌都不相交. 二分匹配 最小点覆盖=最大匹配. 链接:点我 坐标点作为匹配的端点 #inclu ...
- hdu 5831 Rikka with Parenthesis II 线段树
Rikka with Parenthesis II 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5831 Description As we kno ...
- hdu 5742 It's All In The Mind 水题
It's All In The Mind 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5742 Description Professor Zhan ...