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. 编写简单的spring mvc程序,在tomcat上部署

    编写简单的spring mvc程序,在tomcat上部署 1 用java 配置spring mvc ,可以省去web.xmlpackage hello;import org.springframewo ...

  2. [js] 数据结构

    var dic = {"127.0.0.1":{"1440":[["keyx","keyy","clix&qu ...

  3. HTTP(HyperText Transport Protocol)超文本传输协议的状态码

    关于HTTP状态码:是用于表示网页服务器HTTP响应状态的3位数字代码. 所有状态码的第一个数字代表了响应的五种状态之一. 1xx:消息:这一类型的状态码代表请求已被接受,需要继续处理 2xx:成功: ...

  4. CodeForces 723E One-Way Reform

    构造. 有一种十分巧妙的方法可以使图中所有度数为偶数的节点,经过每条边定向后,出度和入度都相等. 首先统计每个节点的度数,将度数为奇数的节点与编号为$n+1$的节点连边,这样一来,这张新图变成了每个节 ...

  5. Git的入门级玩法

    作为一个还没有实际开发经验的程序员,对于版本控制多少有些陌生,我通常的做法就是项目名后面加数字标记版本,然后备注一个文档说明更新.据个人了解svn用的比较多,我也学习了一点,无非是本地下载一个服务器端 ...

  6. NOIP2018 提高组题解

    Day1 T1 据说是原题积木大赛,但是考场上蠢了,只会写数据结构,于是写了一个线段树\(+\)堆\(+\)贪心,先选出最小的,然后区间修改,然后把左右两端区间的最小值丢进堆里,不停从堆中去最小值更新 ...

  7. MTK平台源码 mt6589-jb3-x_20131122

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha

  8. JavaScript的程序构成

    JavaScript的程序构成主要从以下几方面做个总结:控制语句.函数 .事件驱动及事件处理,希望对读者有些帮助. 控制语句: if条件语句 基本格式 if(表述式) 语句段1: ...... els ...

  9. [Nescafé41]编码病毒(循环卷积)

    题意看起来好麻烦实际上很简单,首先4s可以先bitset暴力一下,听说卡卡就能过:$O(2^{22}+n^2/32)$ #include<cstdio> #include<bitse ...

  10. Gauss 消元(模板)

    /* title:Gauss消元整数解/小数解整数矩阵模板 author:lhk time: 2016.9.11 没学vim的菜鸡自己手打了 */ #include<cstdio> #in ...