通知栏消息(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,标记为id
public
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) ...
随机推荐
- 德卡Z90读卡器读取社保卡,德卡Z90读卡器CSharp示例程序源码
前言,最近学习调用 医保卡业务,使用德卡读卡器,主要就是调用一个DLL,动态库文件. 借着自学的机会把心得体会都记录下来,方便感兴趣的小伙伴学习与讨论. 内容均系原创,欢迎大家转载分享,但转载的同时别 ...
- Java第三阶段学习(八:网络通信协议、UDP与TCP协议)
一.网络通信协议 1.概念: 通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,在计算机网络中,这些连接和通信的规则被称为网络通信协议,它对数据的传 ...
- 【LOJ】#2534. 「CQOI2018」异或序列
题解 每个数都处理成前缀和,就相当于问\([l - 1,r]\)有几个数对\(x,y\),\(sum[x] ^ sum[y] = k\) 直接莫队即可 代码 #include <bits/std ...
- django 不能访问静态资源的解决办法
最近在中文win10下使用python的django搭建web测试服务器,发现一个诡异的现象,正常配置好django的模型,视图和模板, 1.setting.py内容如下: ""& ...
- 004.LVM缩减
一 缩减步骤 卸载挂载点 检查文件系统 调整分区大小 缩减LV大小 重新挂载并检查 注意: 1 减少文件的大小一定需要按照上面提高的4个规定动作顺序来做,在缩减LV大小前,首先要缩减filesyste ...
- 前端Hack之XSS攻击个人学习笔记
简单概述 ** 此篇系本人两周来学习XSS的一份个人总结,实质上应该是一份笔记,方便自己日后重新回来复习,文中涉及到的文章我都会在末尾尽可能地添加上,此次总结是我在学习过程中所写,如有任 ...
- k8s+Jenkins+GitLab-自动化部署asp.net core项目
0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 此文阅读目录: 1.闲聊 ...
- WEB应用从服务器主动推送Data到客户端有那些方式?
1) html5 websocket 2) WebSocket 通过 Flash 3) XHR长时间连接 4) XHR Multipart Streaming 5) 不可见的Iframe 6 ...
- OpenJ_POJ C16D Extracurricular Sports 打表找规律
Extracurricular Sports 题目连接: http://acm.hust.edu.cn/vjudge/contest/122701#problem/D Description As w ...
- MikroTik RouterOS电子克隆盘原理收集
终于搞定RouteROS8位电子盘克隆,发个讯息出来分享一下. 不需要付费的免费分享,也没要刻意挡人财路:只是让信息流通一下. 也请看到的人不要用这个方式去赚钱,不然MikroTik还是会再反制的. ...