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的使用的更多相关文章

  1. Android之Notification介绍

    Notification就是在桌面的状态通知栏.这主要涉及三个主要类: Notification:设置通知的各个属性. NotificationManager:负责发送通知和取消通知 Notifica ...

  2. [Android Pro] Android 4.3 NotificationListenerService使用详解

    reference to : http://blog.csdn.net/yihongyuelan/article/details/40977323 概况 Android在4.3的版本中(即API 18 ...

  3. Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)

    在android的应用层中,涉及到很多应用框架,例如:Service框架,Activity管理机制,Broadcast机制,对话框框架,标题栏框架,状态栏框架,通知机制,ActionBar框架等等. ...

  4. android通知栏Notification点击,取消,清除响应事件

    主要是检测android通知栏的三种状态的响应事件 这次在实现推送需求的时候,要用到android通知栏Notification点击后进入消息页面,因为要实现一个保存推送用户名字的功能,我在点击后处理 ...

  5. android通知-Notification

    android中,当app需要向发送一些通知,让使用者注意到你想要告知的信息时,可以用Notification.下面,就来讨论一下,Notification的用法,我们从实际的小例子来进行学习. 1. ...

  6. Android --通知栏Notification

    参考博客:Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它) //创建一个通知栏的Builder构造类 (Create a Notification Bui ...

  7. Android之Notification的多种用法(转)

    我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...

  8. 【转】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 ...

  9. Android之Notification的多种用法

    我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. 我们也 ...

随机推荐

  1. AC日记——[SDOI2009]HH的项链 洛谷 P1972

    [SDOI2009]HH的项链 思路: 莫队: 代码: #include <bits/stdc++.h> #define maxn 100005 #define maxm 400005 # ...

  2. hh:mm:ss时间格式那些事儿

    怎么把hh:mm:ss.45 时间格式换算成秒? 比较简单点的格式,比如hh:mm:ss是比较容易的,但是怎么样把hh:mm:ss.45,这样的格式,就是秒不是整数的时间格式换算成秒? ans:将时间 ...

  3. jq函数绑定与解绑

    最近学到几个新的jq函数 1.bind()绑定函数 2.unbind()解绑函数 3.add() .给元素追加字符串 4.addClass() 给某元素增加class属性值

  4. 【C语言期末实训】学生学籍管理系统

    目录: 一,设计要求 ,总体要求: ,具体功能: 二,设计框架 三,程序代码 ,声明函数和头文件 ,声明结构体 ,声明全局变量 ,主体启动函数 ,主菜单函数 ,创建学生档案函数 ,编辑学生档案函数 , ...

  5. Mac下配置PHP支持GD库FreeType

    一句话脚本 curl -s http://php-osx.liip.ch/install.sh | bash -s 5.6 记得要FQ哦. 或者下面代码保存成.sh ,代码从http://php-os ...

  6. Java中的对象池技术

    java中的对象池技术,是为了方便快捷地创建某些对象而出现的,当需要一个对象时,就可以从池中取一个出来(如果池中没有则创建一个),则在需要重复重复创建相等变量时节省了很多时间.对象池其实也就是一个内存 ...

  7. Spring和依赖注入的价值

    javaeye上看到有帖子,置疑spring和依赖注入的价值,回复内容整理如下: 依赖注入对设计有利,而spring则促进了依赖注入的使用. 如果业务处理类,它所使用的倚赖,都是依靠在这个类内部实现或 ...

  8. luogu P1016 旅行家的预算

    题目描述 一个旅行家想驾驶汽车以最少的费用从一个城市到另一个城市(假设出发时油箱是空的).给定两个城市之间的距离D1.汽车油箱的容量C(以升为单位).每升汽油能行驶的距离D2.出发点每升汽油价格P和沿 ...

  9. 【动态规划】Codeforces Round #406 (Div. 2) C.Berzerk

    有向图博弈问题. 能转移到一个必败态的就是必胜态. 能转移到的全是必胜态的就是必败态. 转移的时候可以用队列维护. 可以看这个 http://www.cnblogs.com/quintessence/ ...

  10. 【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps

    我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是, ...