一、正常的前台Service

我们都知道,Service几乎都是在后台运行的,所以Service的系统优先级还是比较低的,当系统出现内存不足情况时,就有可能回收掉正在后台运行的Service。如果你希望Service可以一直保持运行状态,而不会由于系统内存不足的原因导致被回收,那么就要提高Service的优先级,而提高优先级的方法有多种,其中一种就是考虑使用前台Service。

如何把Service设置为前台Service?很简单,使用startForeground即可。要取消前台,使用stopForeground即可。

不多说,直接上代码,非常的简单,不解释:

public class MyService extends Service {

private static final String TAG = "wxx";

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

Log.d(TAG, "MyService: onCreate()");

//定义一个notification

Notification notification = new Notification();

Intent notificationIntent = new Intent(this, MainActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(this, "My title", "My content", pendingIntent);

//把该service创建为前台service

startForeground(1, notification);

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// TODO Auto-generated method stub

Log.d(TAG, "MyService: onStartCommand()");

return super.onStartCommand(intent, flags, startId);

}

}

有留心的朋友会发现,每当启动该前台Service的时候,手机都会收到一个通知,下拉通知栏,会看到一个通知如“XXService正在运行。”,如下图:

查看文档,知道,当SDK<18时,系统不会有该通知,当SDK>=18时,系统有显示该通知。系统显示该通知,应该是为了防止滥用“startForeground”。

那,如果我就是不想显示该通知给用户?怎么搞?

二、去掉startForeground的通知

本人之前因接触过一些通知相关的内容,于是,大胆假设:把2个同进程的Service都用startForeground设置为前台进程,但他们使用相同的Notification ID,那么他们只会产生一个通知,然后把其中一个Service取消前台效果,那么就会把通知关闭,剩下的那个Service就是前台Service了,而且通知栏没有通知。

有了假设,当然就要验证是否可行~~

看代码吧。。。

先看最后要保留的那个Service的代码:

public class MyService extends Service {

private static final String TAG = "wxx";

private final int PID = android.os.Process.myPid();

private AssistServiceConnection mConnection;

@Override

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onCreate() {

// TODO Auto-generated method stub

super.onCreate();

Log.d(TAG, "MyService: onCreate()");

setForeground();

}

@Override

public int onStartCommand(Intent intent, int flags, int startId) {

// TODO Auto-generated method stub

Log.d(TAG, "MyService: onStartCommand()");

return super.onStartCommand(intent, flags, startId);

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

Log.d(TAG, "MyService: onDestroy()");

}

public void setForeground() {

// sdk < 18 , 直接调用startForeground即可,不会在通知栏创建通知

if (VERSION.SDK_INT < 18) {

this.startForeground(PID, getNotification());

return;

}

if (null == mConnection) {

mConnection = new AssistServiceConnection();

}

this.bindService(new Intent(this, AssistService.class), mConnection,

Service.BIND_AUTO_CREATE);

}

private class AssistServiceConnection implements ServiceConnection {

@Override

public void onServiceDisconnected(ComponentName name) {

Log.d(TAG, "MyService: onServiceDisconnected");

}

@Override

public void onServiceConnected(ComponentName name, IBinder binder) {

Log.d(TAG, "MyService: onServiceConnected");

// sdk >=18

// 的,会在通知栏显示service正在运行,这里不要让用户感知,所以这里的实现方式是利用2个同进程的service,利用相同的notificationID,

// 2个service分别startForeground,然后只在1个service里stopForeground,这样即可去掉通知栏的显示

Service assistService = ((AssistService.LocalBinder) binder)

.getService();

MyService.this.startForeground(PID, getNotification());

assistService.startForeground(PID, getNotification());

assistService.stopForeground(true);

MyService.this.unbindService(mConnection);

mConnection = null;

}

}

private Notification getNotification() {

// 定义一个notification

Notification notification = new Notification();

Intent notificationIntent = new Intent(this, MainActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,

notificationIntent, 0);

notification.setLatestEventInfo(this, "My title", "My content",

pendingIntent);

return notification;

}

再看那个辅助消除通知的Service的代码,非常的简单:

public class AssistService extends Service {

private static final String TAG = "wxx";

public class LocalBinder extends Binder {

public AssistService getService() {

return AssistService.this;

}

}

@Override

public IBinder onBind(Intent intent) {

Log.d(TAG, "AssistService: onBind()");

return new LocalBinder();

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

Log.d(TAG, "AssistService: onDestroy()");

}

}

代码就这么多,上面大体思路是:一个最后保留的MyService,一个辅助消除通知的AssistService, 利用

MyService去绑定AssistService,在关联函数onServiceConnected()中实现两个Service调用startForeground变为前台服务,注意一定要使用一样的Notification ID,然后AssistService取消前台效果stopForeground从而删除通知。

运行上面的代码,发现通知栏真的没有显示通知,成功了? 还要看看两个Service的生命周期是否正常,从日志Log可以看出,两个服务正常,最后只剩下MyService这个服务,而AssistService会destroy掉:

02-15 17:13:01.791: D/wxx(4659): MyService: onCreate()

02-15 17:13:01.801: D/wxx(4659): MyService: onStartCommand()

02-15 17:13:01.801: D/wxx(4659): AssistService: onBind()

02-15 17:13:01.806: D/wxx(4659): MyService: onServiceConnected

02-15 17:13:01.836: D/wxx(4659): AssistService: onDestroy()

看来这种方法可行~~

如果那位大神有更好的方法,希望能分享分享,小弟先谢过了!!

以上就介绍了Android的startForeground前台Service如何去掉通知显示,包括了方面的内容,希望对Android开发有兴趣的朋友有所帮助。 原文链接:http://www.codes51.com/article/detail_322613_1.html

android隐藏前台服务通知,Android的startForeground前台Service如何去掉通知显示相关推荐

  1. android隐藏软键盘方法,Android显示和隐藏软键盘方法

    InputMethodManager类 Android中软键盘的管理主要是通过InputMethodManager类来完成的. InputMethodManager对象的获取方法如下: 获取到Inpu ...

  2. android 命令启动服务,在Android命令行启动程序的方法

    在Android中,除了从界面上启动程序之外,还可以从命令行启动程序,使用的是命令行工具am. 启动的方法为 # am start -n 包(package)名/包名.活动(activity)名称 启 ...

  3. android模拟器socket服务端,android 模拟器跟电脑服务器端用socket通讯

    android 模拟器和电脑服务器端用socket通讯 之前一直会用sockets来实现emulator和PC进行通讯,卡了几天,最后请教其他人终于可以连接了.  错误原因是在IP和端口,IP要用本机 ...

  4. android隐藏app应用程序,Android开发:怎样隐藏自己的app应用

    本文主要介绍怎样通过改动AndroidManifest.xml清单文件来达到隐藏自身应用的目的,不是隐藏第三方应用.为了不浪费大家时间.特此说明. 第一种 改动Activity标签下的节点下的cate ...

  5. Android隐藏导航栏按键,Android如何控制导航栏单个按键的显隐状态

    我们都知道Android系统的导航栏通常有三个按键,分别是BACK, HOME, APP_SWITCH. 网上很多有关导航栏和状态栏显隐的文章,但几乎都是控制导航栏或状态栏所有按键同时显示或消失,如果 ...

  6. android隐藏软键盘方法,Android使用InputMethodManager显示和隐藏软键盘

    Android主要用InputMethodManager来对软键盘进行管理.手动显示或隐藏软键盘前需要先获取InputMethodManager. InputMethodManager imm = ( ...

  7. android自动启动无障碍服务,Xamarin.Android:如何开启无障碍服务永久

    我写的辅助服务,以避免USSD请求过程中AlertWindow开幕:Xamarin.Android:如何开启无障碍服务永久 [Service(Label = "BalanceAccessib ...

  8. android授权新浪微博 服务端,Android 集成新浪微博分享及授权

    本文使用的开发的环境是 eclipse 本文叙述的新浪微博分享及其授权的内容分为两块 : 一 新浪提供的 WeiboSDKDemo 二 嵌入到自己的应用当中去 第一部分 运行成功 WeiboSDKDe ...

  9. android微信支付服务端,Android 微信支付返回-1

    本地服务端测试是可以拉起微信支付的,但是项目移到服务器上就不行呢?开放平台的app签名已经改了发布版的(签名是通过微信官方给的Gen_Signature_Android.apk获取的),appid没换 ...

  10. android中停止服务,在Android中停止服务

    在这里,我尝试了简单的服务程序.启动服务可以正常工作并生成Toast,但停止服务则不能.此简单服务的代码如下: public class MailService extends Service { @ ...

最新文章

  1. $@ 与 $* 差在哪?
  2. A - 排名 HDU - 1236 sort(cmp)
  3. 反编译工具dnspy的安装与使用;
  4. java面试题二十三 接口
  5. 前端学习(1606):数据请求与json-server
  6. 实例60:python
  7. 如何阅读一本书 pdf_《如何阅读一本书》:一本书,四个层次,看阅读小白如何逆袭?...
  8. 12篇文章带你逛遍主流分割网络
  9. ntp同步 mysql_解析Mysql 主从同步延迟原理及解决方案
  10. 高速收发器之8B/10B编码
  11. 操作系统内存管理-原理
  12. 中学计算机基础知识,初中信息技术学业水平考试计算机基础知识考点大全(重点汇总)...
  13. 张量分析初步和矢量恒等式
  14. 如何使用Bootbox
  15. IE的Internet选项“自定义级别”置灰,不能修改的问题解决
  16. 腾讯启动校园招聘人才
  17. python编程求导数_SciPy函数求导数
  18. 浅析错误:software IO TLB: coherent allocation failed for device
  19. UltraEdit 21.30.1006.0 繁体中文破解版(功能最强的文本编辑器)
  20. vue系列教程之微商城项目|项目介绍

热门文章

  1. 应用MATLAB求解线性代数题目(五)——特征值与特征向量
  2. 工业智能网关BL110应用之四十三:网口采集欧姆龙PLC的配置
  3. android手机脱网分析,网络营销-13款手机浏览器分析(Android).pptx
  4. 软件架构模式之管道-过滤器模式--分析
  5. 一文看懂外汇风险准备金率调整为 20%的含义
  6. 面试官:你们的redis主要用来做什么?
  7. 能上QQ无法打开网页
  8. nios2 c语言编程方法nios2系列教程,nios2 c语言编程方法
  9. 苹果AppStore审核,技术支持网址不通过被拒绝
  10. IDEA 顶部导航栏(Main Menu)不见了怎么办?