通知栏消息(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) ...
随机推荐
- .NetCore下使用Polly结合IHttpClientFactory实现聚合服务
在使用微服务的过程中经常会遇到这样的情况,就目前我遇到的问题做下分析 情况一: 这里服务对于前后端分离情况来说,多使用查询服务,前端直接获取不同服务的数据展示,如果出现其中的服务失败,对业务数据无影响 ...
- TypeScript的HTML5游戏
wildfirecode 自动化的基于TypeScript的HTML5游戏开发 自动化的开发流程 在HTML5游戏开发或者说在Web客户端开发中,对项目代码进行修改之后,一般来说,需要手动刷新浏览器来 ...
- 【LOJ】#2076. 「JSOI2016」炸弹攻击
题解 我冷静一下,话说如果去掉建筑和R的限制好像是模拟退火吧 然后开始写模拟退火了,起始点就随机一个敌人作为起始点 没对着数据写了一下获得了70pts,感到美滋滋 然后对着数据卡了很久--发现有个数据 ...
- 【Java】 大话数据结构(14) 排序算法(1) (冒泡排序及其优化)
本文根据<大话数据结构>一书,实现了Java版的冒泡排序. 更多:数据结构与算法合集 基本概念 基本思想:将相邻的元素两两比较,根据大小关系交换位置,直到完成排序. 对n个数组成的无序数列 ...
- Python3 决策树ID3算法实现
# -*- coding: utf-8 -*- """ Created on Wed Jan 24 19:01:40 2018 @author: markli 采用信息增 ...
- 朴素贝叶斯算法--python实现
朴素贝叶斯算法要理解一下基础: [朴素:特征条件独立 贝叶斯:基于贝叶斯定理] 1朴素贝叶斯的概念[联合概率分布.先验概率.条件概率**.全概率公式][条件独立性假设.] 极大似然估计 ...
- android 多线程的实现方式
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha. 1,通过创建线程池,来创建多个线程. 2,通过异步任务,来创建线程池,从而创建多线程. ...
- [JSOI2015]最小表示
题目大意:尽可能多地去掉一个有向无环图上的边,使得图的连通性不变. 思路:拓扑排序,然后倒序求出每个结点到出度为$0$的点的距离$d$,再倒序遍历每一个点$x$,以$d$为关键字对其出边降序排序,尝试 ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem C. Contest 水题
Problem C. Contest 题目连接: http://codeforces.com/gym/100714 Description The second round of the annual ...
- CentOS下使用LVM进行分区(转)
说明:为什么抄,因为这篇文章图文并茂,所有测试都在CentOS 6和7测试过. 许多Linux使用者安装操作系统时都会遇到这样的困境:如何精确评估和分配各个硬盘分区的容量,如果当初评估不准确,一旦系统 ...