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. Linux用户和用户组的初步知识

    用户组相关:在Linux系统下,当创建一个如叫tom的用户,那么系统同时会创建一个叫tom的用户组linux中用户的信息都是放在/etc/passwd下的,用户的密码经过加密后放在/etc/shado ...

  2. mp4文件格式解析(转载)

    mp4文件格式解析 原作:http://blog.sina.com.cn/s/blog_48f93b530100jz4b.html 目前MP4的概念被炒得很火,也很乱.最开始MP4指的是音频(MP3的 ...

  3. LintCode 13. Implement strStr()

    LintCode 13. Implement strStr() 题目描述 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出 ...

  4. iwebshop 增加页面访问次数实时

    class里面的主控制器初始化时添加如下代码 //更新页面访问次数 $siteConfig = new Config('site_config'); $sFilePath =$siteConfig-& ...

  5. Sqlite的导入导出功能

    导出,使用dump命令 导入,使用read命令 可以直接执行,类似 sqlite xxx.db3 ".read ../sss.sql"

  6. Hadoop中文编码乱码相关问题

    mapreduce程序处理GBK编码数据并输出GBK编码数据, hadoop涉及输出文本的默认输出编码统一用没有BOM的UTF-8的形式,但是对于中文的输出window系统默认的是GBK,有些格式文件 ...

  7. 决策树:ID3与C4.5算法

    1.基本概念 1)定义: 决策树是一个预测模型:他代表的是对象属性与对象值之间的一种映射关系,树中每个节点代表的某个可能的属性值. 2)表示方法: 通过把实例从根结点排列到某个叶子结点来分类实例,叶子 ...

  8. SpringBoot整合SpringBatch实用简例

    SpringBatch主要是一个轻量级的大数据量的并行处理(批处理)的框架. 作用和Hadoop很相似,不过Hadoop是基于重量级的分布式环境(处理巨量数据),而SpringBatch是基于轻量的应 ...

  9. 洛谷——P1748 H数

    P1748 H数 题目背景 无 题目描述 所谓H数,是指只含有2,3,5,7这些质因数的数,如630是H数,而22不是.现在要求输出第n个H数,为了方便起见将H[1]定为1.已知n不超过10000,最 ...

  10. Nginx反代,后端一个IP绑定多个SSL证书,导致连接失败之解决方法:HTTPS和SNI扩展

    默认:SSL协议进行握手协商进行连接的时候,默认是不会发送主机名的,也就是是以IP的形式来进行https连接握手协商的,这就导致一个问题,当一台服务器上有多个虚拟主机使用同一个IP的时候, Nginx ...