通知栏消息(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) ...
随机推荐
- NET-知识点:C#中Equals和==比较
第一.相等性比较 其实这个问题的的本质就是C#的相等比较,相等比较可以分两类: 1.引用相等性,引用相等性指两个对象引用均引用同一基础对象. 2.值相等性,值相等性指两个对象包含相同的一个或多个值,其 ...
- hdu 1700 (圆的内接三角形 要周长最大)
以原点为圆心,给出圆上的一点,要求圆上的另外两点,使得这三个点的距离和最大,很容易想到这是一个等边三角形然后有这两个公式 点a为已知点a*b=|a|*|b|*cos(120); x*x+y*y=r*r ...
- L3-004 肿瘤诊断 dfs bfs
在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是每张切片的尺寸(即每张切片 ...
- 【Java】 大话数据结构(8) 串的模式匹配算法(朴素、KMP、改进算法)
本文根据<大话数据结构>一书,实现了Java版的串的朴素模式匹配算法.KMP模式匹配算法.KMP模式匹配算法的改进算法. 1.朴素的模式匹配算法 为主串和子串分别定义指针i,j. (1)当 ...
- Java—Math类和随机数类
一.Math类(主要封装算数运算的静态方法) 定义: Math是没有构造方法的. java语言中提供了一个执行数学基本运算的Math类,Math类包括常用的数学运算和一些数学函数.还提供了一些常用的常 ...
- 命令:mktemp
简介 mktemp命令用于创建一个临时的文件或者目录. 语法格式 mktemp [OPTION]... [TEMPLATE] 示例 不带选项和参数的mktemp用于创建临时文件,带-d选项用于创建临时 ...
- rabbitmq学习(八) —— 可靠机制上的“可靠”
接着上一篇,既然已经有了手动ack.confirm机制.return机制,还不够吗? 以下博文转自https://www.jianshu.com/p/6579e48d18ae和https://my.o ...
- 统计Mongo数组中相同对象的属性之和
统计Mongo数组中相同对象的属性之和 需求 需要统计app端用户的行为,按天分表,存入mongo.每次用户进行操作的时候,将数据存入app本地,下次用户启动的时候,提交存入mongo,删除app本地 ...
- ViewPager 无限循环
Overview 我们在使用ViewPager来制作图片轮播的时候,常常为ViewPager不能一直无限循环的问题所苦恼.对于这个问题,目前从网上找到了两个思路来解决: 将 ViewPager 的Co ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem I. Plugs and Sockets 费用流
Problem I. Plugs and Sockets 题目连接: http://www.codeforces.com/gym/100253 Description The Berland Regi ...