[Android Pro] Notification的使用
Android 4.0以前:
1: 普通的notification
private static final int NOTIFY_ID = 0;
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
private void showNotification(Store store) {
Notification notification = new Notification();
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_ALL;
notification.icon = R.drawable.ic_launch;
notification.when = System.currentTimeMillis(); Intent intent = new Intent(this,AlarmActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("store", store);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, PendingIntent.FLAG_UPDATE_CURRENT);//FLAG_ONE_SHOT //Change the name of the notification here
notification.setLatestEventInfo(this, store.getStoreName()+"("+store.getDistance()+")", store.getAddress(), contentIntent);
notificationManager.notify(NOTIFY_ID, notification);
}
2: 将服务service设置为前台notification
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
setServiceForground();
}
@SuppressWarnings("deprecation")
public void setServiceForground() {
Notification notification = new Notification(R.drawable.ic_launcher,
"my_service_name", System.currentTimeMillis());
//要添加newtask
PendingIntent p_intent = PendingIntent.getActivity(this, 0, new Intent(
this, MainActivity.class), 0);
notification.setLatestEventInfo(this, "MyServiceNotification",
"MyServiceNotification is Running!", p_intent);
startForeground(0x1982, notification);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Android 4.0 以后:
将服务service设置为前台notification
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
setServiceForground();
}
@Override
public void onDestroy() {
super.onDestroy();
}
public void setServiceForground() {
Notification.Builder build = new Notification.Builder(this);
// PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(
// this, MainActivity.class), 0);
// build.setContentIntent(contentIntent);
build.setSmallIcon(R.drawable.ic_launcher);
build.setContentTitle("MyServiceNotification");
build.setContentText("MyServiceNotification is Running!");
startForeground(0x1982, build.build());
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
[Android Pro] Notification的使用的更多相关文章
- Android之Notification介绍
Notification就是在桌面的状态通知栏.这主要涉及三个主要类: Notification:设置通知的各个属性. NotificationManager:负责发送通知和取消通知 Notifica ...
- [Android Pro] Android 4.3 NotificationListenerService使用详解
reference to : http://blog.csdn.net/yihongyuelan/article/details/40977323 概况 Android在4.3的版本中(即API 18 ...
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...
- android通知栏Notification点击,取消,清除响应事件
主要是检测android通知栏的三种状态的响应事件 这次在实现推送需求的时候,要用到android通知栏Notification点击后进入消息页面,因为要实现一个保存推送用户名字的功能,我在点击后处理 ...
- android通知-Notification
android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...
- Android --通知栏Notification
参考博客:Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它) //创建一个通知栏的Builder构造类 (Create a Notification Bui ...
- Android之Notification的多种用法(转)
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...
- 【转】can't find referenced method 'android.app.RemoteInput[] getRemoteInputs()' in class android.app.Notification$Action
原文网址:http://stackoverflow.com/questions/25508735/cant-find-referenced-method-android-app-remoteinput ...
- Android之Notification的多种用法
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...
随机推荐
- 关于Logstash中grok插件的正则表达式例子
一.前言 近期需要对Nginx产生的日志进行采集,问了下度娘,业内最著名的解决方案非ELK(Elasticsearch, Logstash, Kibana)莫属. Logstash负责采集日志,Ela ...
- Windows 10 安装 Mongodb
因为新换了Windows 10 电脑,需要在新电脑重新安装所有的软件,包括mongodb 下载文件:首先在mongodb的官方网站上下载最新版本的mongodb安装程序,https://www.mon ...
- Nodejs Mocha测试学习
参考大神阮一峰的文章<测试框架 Mocha 实例教程> 目前在使用Nodejs,但写完的程序很容易出错,那怎么办?需要引入单元测试去做基础的测试 目前Nodejs用来做单元测试的工具接触的 ...
- thinkphp下实现ajax无刷新分页
1.前言 作为一名php程序员,我们开发网站主要就是为了客户从客户端进行体验,在这里,thinkphp框架自带的分页类是每次翻页都要刷新一下整个页面,这种翻页的用户体验显然是不太理想的,我们希望每次翻 ...
- [hdu-4946] Area of Mushroom 计算几何 凸包
大致题意: 平面上有n个人,给你每个人的坐标和一个速度v,如果某个人比其他所有人都先到达某点,则该点就被这个人掌控,求谁掌控者无限大的面积. 首先 速度最大的人,抛弃其他人,速度小的人必定无法得到无限 ...
- 洛谷P1197 [JSOI2008] 星球大战 [并查集]
题目传送门 星球大战 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这 ...
- Codeforces Beta Round #13 E. Holes (分块)
E. Holes time limit per test 1 second memory limit per test 64 megabytes input standard input output ...
- 【数据结构】 最小生成树(三)——prim算法
上一期介绍到了kruskal算法,这个算法诞生于1956年,重难点就是如何判断是否形成回路,此处要用到并查集,不会用当然会觉得难,今天介绍的prim算法在kruskal算法之后一年(即1957年)诞生 ...
- Electron与jQuery中$符号冲突的三种解决方法
在Electron工程中引用jQuery时,经常会出现以下错误: Uncaught ReferenceError: $ is not defined 解决的具体方法如下: ①.在测试的过程中(测试过1 ...
- Grunt Gulp Browserify Webpack
Grunt 是相比后面几个更早的项目,他依赖于各种插件的配置.这是一个很好的解决方案,但是请相信我,你不会想看到一个 300 行的 Gruntfile Gulp 提供了一个不一样的解决方案,而不是依赖 ...