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. 【JBPM4】任务form表单

    主要是在任务节点处保存一个链接地址(表单),方便在执行到此时取值,访问. 好处是XML可随时修改,部署. JPDL <?xml version="1.0" encoding= ...

  2. CentOS7安装配置WPS

    1.下载 地址:http://wps-community.org/downloads 2.安装 rpm -ivh wps-office-10.1.0.5707-1.a21.x86_64.rpm 3.运 ...

  3. python打印所有汉字

    n=0 for ch in xrange(0x4e00, 0x9fa6): print unichr(ch), n = n+1 if(n%50==0): print '\n' print n

  4. centos xampp 隐藏phpmyadmin地址

    /opt/lampp/etc/extra/httpd-xampp.conf Alias /phpmyadmin "/opt/xampp/phpMyAdmin/" 改为 Alias ...

  5. mysql查看表结构,字段等命令

    mysql查看表结构命令,如下: desc 表名; show columns from 表名; describe 表名; show create table 表名;

  6. AutoHotKey 常用脚本

    ; IMPORTANT INFO ABOUT GETTING STARTED: Lines that start with a ; semicolon, such as this one, are c ...

  7. [BZOJ4007][JLOI2015]战争调度(DP+主定理)

    第一眼DP,发现不可做,第二眼就只能$O(2^{1024})$暴搜了. 重新审视一下这个DP,f[x][i]表示在x的祖先已经全部染色之后,x的子树中共有i个参战平民的最大贡献. 设k为总结点数,对于 ...

  8. 2016ACM-ICPC网络赛北京赛区 1001 (trie树牌大模拟)

    [题目传送门] 1383 : The Book List 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 The history of Peking University ...

  9. BZOJ 1098 [POI2007]办公楼biu(反向图bfs+并查集优化)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1098 [题目大意] 现在有一张图,要求将这张图的点划分为尽量多的分组,对于不同分组的两 ...

  10. [转载]c++常用字符串操作函数

    原文地址:c++常用字符串操作函数作者:Valsun 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source ...