Android如何隐藏掉前台服务的三星s8通知栏隐藏,史上详

将一个服务改为前台服务
1.修改Service的onCreate方法代码public class MyService extends Service {
public void onCreate() {
super.onCreate();
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentText("message")
.setContentTitle("title")
.setContentIntent(pendingIntent);
Notification notification = builder.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// manager.notify(1, notification);
startForeground(1,notification);
public IBinder onBind(Intent intent) {
}使用了notification ,区别是用startForeground方法,这样打开的notification常驻状态栏(除非进程或者该服务被杀死)。开启一个不显示的notification , 第一个参数id为0即可 :startForeground(0, new Notification());要从前台删除服务,需要调用stopForeground()方法,这个方法需要一个布尔型参数,指示是否删除状态栏通知。这个方法不终止服务。但是,如果你终止了正在运行的前台服务,那么通知也会被删除。
没有更多推荐了,如何点击通知栏中的前台服务,返回当前正在进行的活动
刚开始编写的时候在处理PendingIntent时,就是简单的让所打开的intent
new出想回到的活动,但是后来发现这样实际上只是新建了一个相同的活动覆盖住了正在进行的活动上,就像写的音乐播放器,在后台的时候若我点击通知栏里的前台服务,会打开了一个新的音乐播放器活动覆盖在正在播放音乐的活动的音乐上面,并不是把后台正在进行的音乐活动调出来。最后解决方法如下: &pre name="code" class="java"&NotificationCompat.Builder builder = new Builder(this);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName(this.getPackageName(),
this.getPackageName() + "." + this.getLocalClassName()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
// 点击跳转到主界面
PendingIntent intent_go = PendingIntent.getActivity(this, 5, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.notice, intent_go);
核心是先隐式启动Activity,先setAction,再设置启动模式
没有更多推荐了,Android服务——前台服务
我们通常创建的服务都是运行在后台的,但是当我们遇到这样的场景的时候:一个播放音乐的音乐播放器服务应该被设置在前台运行,因为用户要明确的知道它们的操作。状态栏中的通知需要指明了当前的歌曲,并且用户启动一个跟这个音乐播放器交互的Activity。
这个时候我们需要创建前台服务。
1. 创建前台服务
我们需要实现我们的服务类 需要提供一个通知栏图标,并且调用startForeground(API
level 5 之前的需要使用setForeground())需要注册服务,在xml配置service
2. ForegroundServer.java
1). 我们在onStartCommand函数中调用displayNotification来展示前台服务的描述
public int onStartCommand(Intent intent, int flags, int startId) {
Log.w(TAG, "onStartCommand...");
//设置成前台任务
displayNotification();
return super.onStartCommand(intent, flags, startId);
2). displayNotification调用startForeground
private void displayNotification()
Intent intent = new Intent(TAG);
PendingIntent piResult0 = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Notification noti = new Notification.Builder(ForegroundServer.this)
.setContentTitle("前台服务运行中...")
.setContentText("点击取消")
.setSmallIcon(android.R.drawable.btn_star)
.setContentIntent(piResult0)
ForegroundServer.this.startForeground(1, noti);
Toast.makeText(this, "开启前台服务", Toast.LENGTH_LONG).show();
3). 启动服务,我们在Activity中调用startService来启动前台服务,部分代码如下:
final Intent intentService = new Intent(MyActivity.this, ForegroundServer.class);
startService(intentService);
BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Toast.makeText(MyActivity.this, "关闭前台服务", Toast.LENGTH_LONG).show();
stopService(intentService);
registerReceiver(broadcastReceiver, new IntentFilter(ForegroundServer.TAG));
当点击通知栏后,会关闭服务。
用startService来开启服务时必须用stopService来关闭服务。(也可以用bindService和unbindService来开启和关闭服务)
没有更多推荐了,Android如何隐藏掉前台服务的通知栏,史上详细的资料!
有些小伙伴会遇到这样的问题:如果想让一个服务在后台长期的运行下去,而且在系统资源不足的情况下不会被系统kill掉,怎么办?这个时候上网google之后会发现,有个叫“前台服务”的东东,貌似很强大,无论怎样都会常驻系统内存。但是,都会发现,在高版本的Android版本中,前台服务一旦运行,就会默认在通知栏显示运行状态,无法手动去除。
有什么好的办法可以让其运行但又不会显示在通知栏的办法吗?
答案肯定有,在我动手实践后,发现了一个绝对有效的办法(也查阅过之前网友的做法,但是好像有漏洞,我这里说明的更详细,会考虑到各个版本,需要的小伙伴直接copy就可以用了),废话不多说,盖茨,上代码!
工程目录:
这里,前台服务名称为ForegroundService,协助我们去掉通知栏的Service为HelpService,入口为MainActivity
package com.example.
import android.os.B
import android.app.A
import android.content.I
import android.view.V
public class MainActivity extends Activity {
public static final String TAG = "MainActivity";
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.
import android.app.N
import android.app.PendingI
import android.app.S
import android.content.ComponentN
import android.content.I
import android.content.ServiceC
import android.os.IB
import android.os.Build.VERSION;
import android.util.L
* 需要开启的前台服务
* @author zhouyang
public class ForegroundService extends Service {
public static final String TAG = "ForegroundService";
private final int PID = android.os.Process.myPid();
private ServiceConnection mC
public IBinder onBind(Intent intent) {
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());
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();
private class CoverServiceConnection implements ServiceConnection {
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "ForegroundService: onServiceDisconnected");
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 =
public void onDestroy() {
super.onDestroy();
stopForeground(true);
HelpService部分:
package com.example.
import android.app.S
import android.content.I
import android.os.B
import android.os.IB
import android.util.L
* 协助去掉通知的服务
* @author zhouyang
public class HelpService extends Service {
private static final String TAG = "HelpService";
public class LocalBinder extends Binder {
public HelpService getService() {
return HelpService.
public IBinder onBind(Intent intent) {
Log.d(TAG, "HelpService: onBind()");
return new LocalBinder();
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "HelpService: onDestroy()");
现在正常卡开启前台服务:
我们来看效果:
现在改为setForeground()启动:
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 = 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())
所以,使用的时候需要注意版本问题,有些API已经过时了,就不要使用啦!
本次就到这里,希望能对你有所帮助~!
没有更多推荐了,51CTO旗下网站
1.2.3 运行在前台的Service
《Android开发进阶从小工到专家》第1章Android的构成基石――四大组件,本章不再介绍Android系统的架构、历史等知识,而是直接切入主题,从讲解Android的四大组件开始,然后一步一步深入学习开发中的重要知识点,使得我们能够从基本原理层面掌握Android开发基础知识。本节为大家介绍运行在前台的Service。
作者:何红辉来源:人民邮电出版社| 09:46
1.2.3 运行在前台的Service
Service默认是运行在后台的,因此,它的优先级相对比较低,当系统出现内存不足的情况时,它就有可能会被回收掉。如果希望Service可以一直保持运行状态,而不会由于系统内存不足被回收,可以将Service运行在前台。前台服务不仅不会被系统无情地回收,它还会在通知栏显示一条消息,下拉状态栏后可以看到更加详细的信息。例如,墨迹天气在前台运行了一个Service,并且在Service中定时更新通知栏上的天气信息,如图1-11所示。
下面我们就来实现一个类似于如图1-11所示的效果,首先我们定义一个服务,代码如下:
public&class&WeatherService&extends&Service&{ &&&&&&private&static&final&int&NOTIFY_ID&=&123; &&&&&&@Override &&&&&public&void&onCreate()&{ &&&&&&&&&super.onCreate(); &&&&&&&&&showNotification(); &&&&&} &&&&&&/** &&&&&&*&在通知栏显示天气信息 &&&&&&*/ &&&&&private&void&showNotification()&{ &&&&&&&&&NotificationCompat.Builder&mBuilder&= &&&&&&&&&&&&&&&&&new&NotificationCompat.Builder(this) &&&&&&&&&&&&&&&&&&&&&&&&&.setSmallIcon(R.drawable.weather) &&&&&&&&&&&&&&&&&&&&&&&&&.setContentTitle(getText(R.string.the_day)) &&&&&&&&&&&&&&&&&&&&&&&&&.setContentText(getText(R.string.weather)); &&&&&&&&&//&创建通知被点击时触发的Intent &&&&&&&&&Intent&resultIntent&=&new&Intent(this,&MainActivity.class); &&&&&&&&&//&创建任务栈Builder &&&&&&&&&TaskStackBuilder&stackBuilder&=&TaskStackBuilder.create(this); &&&&&&&&&stackBuilder.addParentStack(MainActivity.class); &&&&&&&&&stackBuilder.addNextIntent(resultIntent); &&&&&&&&&PendingIntent&resultPendingIntent&= &&&&&&&&&&&&&&&&&stackBuilder.getPendingIntent( &&&&&&&&&&&&&&&&&&&&&&&&&0,&PendingIntent.FLAG_UPDATE_CURRENT); &&&&&&&&&mBuilder.setContentIntent(resultPendingIntent); &&&&&&&&&NotificationManager&mNotifyMgr&= &&&&&&&&&&&&&&&&&(NotificationManager)&getSystemService(Context.NOTIFICATION_SERVICE); &&&&&&&&&//&构建通知 &&&&&&&&&final&Notification&notification&=&mBuilder.build()&; &&&&&&&&&//&显示通知 &&&&&&&&&mNotifyMgr.notify(NOTIFY_ID,&notification); &&&&&&&&&//&启动为前台服务 &&&&&&&&&startForeground(NOTIFY_ID,&notification); &&&&&} &}&
我们在onCreate函数中调用了showNotification函数显示通知,并且在最后调用startForeground将服务设置为前台服务。在AndroidManifest.xml注册之后我们就可以启动该Service了。效果如图1-12所示。
喜欢的朋友可以添加我们的微信账号:
51CTO读书频道二维码
51CTO读书频道活动讨论群:
【责任编辑: TEL:(010)】&&&&&&
大家都在看猜你喜欢
热点热点头条头条热点
24H热文一周话题本月最赞
讲师:413608人学习过
讲师:159200人学习过
讲师:14887人学习过
精选博文论坛热帖下载排行
当前,开源框架层出不穷,它为用户提供了通用的解决方案,同时也增加了用户的学习难度。开源是一把“双刃剑”,一方面它共享了资源,提供了...
订阅51CTO邮刊}

我要回帖

更多关于 三星s8通知栏隐藏 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信