Android如何隐藏掉前台服务的通知栏,史上详细的资料!

deepfacelab下载安装及训练(小白篇),以及导出SAEHD为dfm文件,解决broken pipe问题 其中如果用的是RTX30系列显卡的话,需要把WIN10升级到20H4或更新版本,然后打开硬件加速,不然会出现训练时一直卡在第一个迭代不动了,我的服务器版本为22H2,符合。支持WIN10,win11,WIN7,LINUX,不支持mac(因为mac不支持NVIDIA显卡) 阅读详情

有些小伙伴会遇到这样的问题:如果想让一个服务在后台长期的运行下去,而且在系统资源不足的情况下不会被系统kill掉,怎么办?这个时候上网google之后会发现,有个叫“前台服务”的东东,貌似很强大,无论怎样都会常驻系统内存。但是,都会发现,在高版本的Android版本中,前台服务一旦运行,就会默认在通知栏显示运行状态,无法手动去除。

有什么好的办法可以让其运行但又不会显示在通知栏的办法吗?

答案肯定有,在我动手实践后,发现了一个绝对有效的办法(也查阅过之前网友的做法,但是好像有漏洞,我这里说明的更详细,会考虑到各个版本,需要的小伙伴直接copy就可以用了),废话不多说,盖茨,上代码!


工程目录:



这里,前台服务名称为ForegroundService,协助我们去掉通知栏的Service为HelpService,入口为MainActivity

package com.example.foregroundservice;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

public class MainActivity extends Activity {
	public static final String TAG = "MainActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	public void startForeground(View view) {
		Intent intent = new Intent(this, ForegroundService.class);
		startService(intent);
	}

}


ForegroundService部分:

package com.example.foregroundservice;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Build.VERSION;
import android.util.Log;

/**
 * 需要开启的前台服务
 * 
 * @author zhouyang
 */
public class ForegroundService extends Service {

	public static final String TAG = "ForegroundService";
	private final int PID = android.os.Process.myPid();
	private ServiceConnection mConnection;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.e(TAG, "ForegroundService is running");
		startForeground(PID, getNotification());// 正常启动前台服务
		// setForeground();// 启动前台服务,并隐藏前台服务的通知
	}

	public void setForeground() {
		// sdk < 18 , 直接调用startForeground即可,不会在通知栏创建通知
		if (VERSION.SDK_INT < 18) {
			this.startForeground(PID, getNotification());
			return;
		}

		if (null == mConnection) {
			mConnection = new CoverServiceConnection();
		}

		this.bindService(new Intent(this, HelpService.class), mConnection,
				Service.BIND_AUTO_CREATE);
	}

	private Notification getNotification() {
		// 定义一个notification
		Notification notification = new Notification();
		Intent notificationIntent = new Intent(this, ForegroundService.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
				notificationIntent, 0);
		// notification.setLatestEventInfo(this, "Foreground", "正在运行哦",
		// pendingIntent);

		Notification.Builder builder = new Notification.Builder(this)
				.setAutoCancel(true).setContentTitle("ForegroundService")
				.setContentText("正在运行哦").setContentIntent(pendingIntent)
				.setSmallIcon(R.drawable.ic_launcher)
				.setWhen(System.currentTimeMillis()).setOngoing(true);
		notification = builder.getNotification();
		return notification;
	}

	private class CoverServiceConnection implements ServiceConnection {
		@Override
		public void onServiceDisconnected(ComponentName name) {
			Log.d(TAG, "ForegroundService: onServiceDisconnected");
		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder binder) {
			Log.d(TAG, "ForegroundService: onServiceConnected");

			// sdk >= 18 的,会在通知栏显示service正在运行,这里不要让用户感知,所以这里的实现方式是利用2个同进程的service,利用相同的notificationID,
			// 2个service分别startForeground,然后只在1个service里stopForeground,这样即可去掉通知栏的显示
			Service helpService = ((HelpService.LocalBinder) binder)
					.getService();
			ForegroundService.this.startForeground(PID, getNotification());
			helpService.startForeground(PID, getNotification());
			helpService.stopForeground(true);

			ForegroundService.this.unbindService(mConnection);
			mConnection = null;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		stopForeground(true);
	}
}


HelpService部分:

package com.example.foregroundservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

/**
 * 协助去掉通知的服务
 * 
 * @author zhouyang
 */
public class HelpService extends Service {

	private static final String TAG = "HelpService";

	public class LocalBinder extends Binder {
		public HelpService getService() {
			return HelpService.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.d(TAG, "HelpService: onBind()");
		return new LocalBinder();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "HelpService: onDestroy()");
	}

}

现在正常卡开启前台服务:




我们来看效果:


现在改为setForeground()启动:

@Override
	public void onCreate() {
		super.onCreate();
		Log.e(TAG, "ForegroundService is running");
		// startForeground(PID, getNotification());// 正常启动前台服务
		setForeground();// 启动前台服务,并隐藏前台服务的通知
	}

看效果:


是不是隐藏掉了?没看错,确实隐藏了,具体原因代码里有解释,这里我就不详细说明啦,我们再来看打印日志:


确实,前台服务已经在运行了,证明了我们的猜想。


另外,在使用Notifacation的时候,有几点需注意:


低于API Level 11版本,也就是Android 2.3.3以下的系统中,setLatestEventInfo()函数是唯一的实现方法。前面的有关属性设置这里就不再提了,网上资料很多。

Intent  intent = new Intent(this,MainActivity);  
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);  
notification.setLatestEventInfo(context, title, message, pendingIntent);          
manager.notify(id, notification);  


高于API Level 11,低于API Level 16 (Android 4.1.2)版本的系统中,可使用Notification.Builder来构造函数。但要使用getNotification()来使notification实现。此时,前面版本在notification中设置的Flags,icon等属性都已经无效,要在builder里面设置。

Notification.Builder builder = new Notification.Builder(context)  
            .setAutoCancel(true)  
            .setContentTitle("title")  
            .setContentText("describe")  
            .setContentIntent(pendingIntent)  
            .setSmallIcon(R.drawable.ic_launcher)  
            .setWhen(System.currentTimeMillis())  
            .setOngoing(true);  
notification=builder.getNotification();  

高于API Level 16的版本,就可以用Builder和build()函数来配套的方便使用notification了。

Notification notification = new Notification.Builder(context)    
         .setAutoCancel(true)    
         .setContentTitle("title")    
         .setContentText("describe")    
         .setContentIntent(pendingIntent)    
         .setSmallIcon(R.drawable.ic_launcher)    
         .setWhen(System.currentTimeMillis())    
         .build();   

所以,使用的时候需要注意版本问题,有些API已经过时了,就不要使用啦!

本次就到这里,希望能对你有所帮助~!微笑

无人机通信电台--XBee-PRO 900HP (S3B) XBee PRO S3B也称为XBee-900HP无线模块,它是一款工作在频段900~928MHz之间,基于FHSS跳频技术的远距离无线数传电台核心模块。常用型号如下: 类别 型号 开发套件 XKB9-DMT-UHP XBee-PRO 900HP (S3B) DigiMesh模块,200Kbps,软天线 XBP9B-DMWT-002 XBee-PRO 900HP (S3B)... 阅读详情

相关推荐

Android 系统状态栏的屏蔽与通知栏隐藏

SystemUI 是一个运行在系统级别的应用程序,负责管理状态栏、通知栏以及其他与用户界面相关的功能。通过修改 SystemUI 的配置,我们可以实现定制化的状态栏和通知栏行为。然而,有时候我们可能需要对通知栏进行更高级的控制,比如隐藏通知栏中的特定通知,或者干脆完全屏蔽通知栏的显示。本文将介绍如何使用代码来实现这些功能。综上所述,通过相关的代码实现,我们可以轻松地屏蔽 Android 系统的通知栏显示,并且还能隐藏特定的通知。我们只需调用该方法,并传入要取消的通知的 ID 即可实现隐藏特定通知的功能。

HackDashX的博客 990

Android通知栏前台服务的实现

一、前台服务的简单介绍 前台服务是那些被认为用户知道且在系统内存不足的时候不允许系统杀死的服务前台服务必须给状态栏提供一个通知,它被放到正在运行(Ongoing)标题之下——这就意味着通知只有在这个服务被终止或从前台主动移除通知后才能被解除。 最常见的表现形式就是音乐播放服务,应用程序后台运行时,用户可以通过通知栏,知道当前播放内容,并进行暂停、继续、切歌等相关操作。 二、为什么使用前台服务 后台运行的Service系统优先级相对较低,当系统内存不足时,在后台运行的Service就有可能被回收,为了保持后台服务的正常运行及相关操作,可以选择将需要保持运行的Service设置为前台服务,从

Android12 根据包名屏蔽前台通知显示

有个需求,需要屏蔽个别APP的前台通知显示。

guanmingyuangmy的博客 903

android隐藏前台服务通知,START_STICKY,前台Android服务在没有通知的情况下消失

我在我的新应用程序中启动了一项服务.该服务是有前途的,带有通知.当在AVD 2.1 API Level 7中运行时,一切正常.但是当它在运行Gingerbread的三星Galaxy Tab上运行时,服务将启动(图标和应用程序名称显示在通知区域的顶部),但几秒钟后,服务就会消失.我可以看到的Log中的最后一个条目与我的App相关联,是我的Log.d(“Taglines”,“Return with w...

weixin_31222401的博客 1611

通知栏Android前台进城,Android如何隐藏前台服务通知栏,史上详细资料!

有些小伙伴会遇到这样的问题:如果想让一个服务在后台长期的运行下去,而且在系统资源不足的情况下不会被系统kill,怎么办?这个时候上网google之后会发现,有个叫“前台服务”的东东,貌似很强大,无论怎样都会常驻系统内存。但是,都会发现,在高版本的Android版本中,前台服务一旦运行,就会默认在通知栏显示运行状态,无法手动去除。有什么好的办法可以让其运行但又不会显示在通知栏的办法吗?答案肯定有,...

weixin_33670167的博客 1910

android隐藏前台服务通知,Android的startForeground前台Service如何去通知顯示

關於Android Service的內容,本人上一篇轉載的博客非常詳細,有需要的可以到下面鏈接查看:Android Service 完全解析本文是根據其中某一個知識點擴展出來的。一、正常的前台Service我們都知道,Service幾乎都是在后台運行的,所以Service的系統優先級還是比較低的,當系統出現內存不足情況時,就有可能回收正在后台運行的Service。如果你希望Service可以一直...

weixin_39631316的博客 1456

android 开启前台服务,并隐藏8.0以下的通知

1.首先在activityj里面启动 Intent intent = new Intent(MainActivity.this, MyService.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { startForegroundService(intent ); } else { startServ...

qq_30089721的博客 1668

android隐藏前台服务通知,Android的startForeground前台Service如何去通知显示

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

weixin_34125336的博客 4182

Android前台服务

前台服务是一种在 Android 应用程序中执行长时间运行任务的服务类型。与普通的后台服务不同,前台服务在系统通知栏中显示一个可见的通知,向用户表明应用程序正在进行某项重要的操作,以便用户知晓并提供更好的用户体验。

Jason_Lee155的博客 4858

android 前台服务自定义布局不显示_Android的startForeground前台Service如何去通知显示...

匿名用户1级2016-10-25 回答首先写一个BootstarpService,顾名思义,这个service只是起引导作用,干完活就退出了。最精华的部分其实就是这句stopSelf(),说白了这个service其实还没起起来就被停了,这样onDestroy()里就会调用stopForeground(),通知栏的常驻通知就会被消。[java]viewplaincopypubliccla...

weixin_32005771的博客 868

Android8.0如何隐藏通知栏“xxx正在运行”

出现的原因是你app里使用了 灰度保活 引起的。你可以看一下你的service,是不是有service的onStartCommand方法是这样写的: if (Build.VERSION.SDK_INT &lt; 18) {               startForeground(GRAY_SERVICE_ID, new Notification());//API &lt; 18 ,此方法能有效...

qq_3316763108的博客 9892

Android10以后,启动前台服务,必须显示通知的处理方案

替代startForegroundService 方案,隐藏前台服务通知栏

qq_26262057的博客 1738

Android 8.0以上通知栏不显示

Android 8.0以上通知栏不显示 通知栏使用重要的API NotificationManager Notification NotificationChannel 最近在android 8.0的手机上发现通知栏不显示通知了! No Channel found for pkg=camera.test.com.perssion, channelId=null, id=1, tag=null, ...

苏打水解渴的博客 2542

Android前台服务通知

Android前台服务通知栏通知的创建

juer2017的博客 3121

Android的startForeground前台Service如何去通知显示

关于Android Service的内容,本人上一篇转载的博客非常详细,有需要的可以到下面链接查看:Android Service 完全解析 本文是根据其中某一个知识点扩展出来的。 一、正常的前台Service 我们都知道,Service几乎都是在后台运行的,所以Service的系统优先级还是比较低的,当系统出现内存不足情况时,就有可能回收正在后台运行的Service。如果你希望S

wxx614817的专栏 4万+

android隐藏前台服务通知,如何在Android中更新前台服务通知文本?

如果要更新startForeground()设置的通知,只需构建新通知,然后使用NotificationManager通知它。关键是使用相同的通知ID。我没有测试反复调用startForeground()来更新Notification的场景,但我认为使用NotificationManager.notify会更好。更新通知不会从前台状态中删除服务(这只能通过调用stopForground来完成);例...

weixin_42535461的博客 618

android 通知消失了,通知消失 - Android DownloadManager

解决方案:需要API 11,请参阅下面的答案!简单问题:使用实施的DownloadManager下载文件后,通知消失.下载后如何强制通知保留?我尝试使用VISIBILITY_VISIBLE_NOTIFY_COMPLETED,但我不知道如何使用它感谢任何帮助解决这个问题;)编辑:代码public class BgDL extends Activity {private DownloadManager...

weixin_40005795的博客 469

android 隐藏通知栏,android  framework层隐藏状态通知栏

转自:http://blog.csdn.net/f24762/article/details/42582433我们主要是想通过隐藏StatusBar来消除通知栏,在%Android_Source%/frameworks/base/packages/SystemUI/src下我们可以找到com.android.systemui.statusbar包下面的BaseStatusBar.;查看这个抽象方法...

weixin_36277530的博客 692

android隐藏前台服务通知,在点击通知操作上隐藏Foreground Service的通知

我有一个具有前端通知前台服务的警报应用程序,该通知有两个操作,其中一个向服务发送意图并可以根据应用程序配置打开活动。问题是,当我点击将意图发送到服务的操作时,通知不会隐藏。 当intent打开Activity时,似乎不会发生这种情况我不想要没有通知前台服务,我只是希望它在将意图发送到服务时将其隐藏通知抽屉这是代码:NotificationCompat.Builder(mAlarmApplic...

weixin_32112607的博客 1594

涪江.zip

三级水系流域矢量数据,数据格式shp格式,坐标系wgs84,真实可靠可打开,放心使用

上一篇: 代码浅析 Android Lock 、ReentrantLock线程锁及其作用
下一篇: Android解决进程间通信,线程同步的问题
兮谁与歌
博客等级 码龄10年 18粉丝 13原创
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值