活动可见时隐藏前台服务通知 [英] Hide notification of foreground service while activity is visible

查看:134
本文介绍了活动可见时隐藏前台服务通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

他们是一种将服务作为前台服务启动并在活动可见时隐藏通知的方法吗?

Is their a way to start a service as a foreground service and hide the notification while an activity is visible?

考虑一个音乐播放器,在打开应用程序时,您不需要该通知(带有按钮),但是只要音乐播放器在后台,都应显示该通知.

Consider a music player, while the app is opened, you don't need the notification (with i.e. the buttons), but whenever the music player is in the background, the notification should be shown.

我知道,如果我不在前台运行我的服务,该怎么做...但是当在前台运行时,服务本身需要通知并显示它,而我不能自己管理通知...

I know, how to do this, if I DON'T run my service in foreground... But when running in foreground, the service itself needs the notification and shows it and I can't manage the notification by myself...

我该如何解决这个问题?

How can I solve that problem?

推荐答案

您可以这样做.此方法的先决条件是,您的活动必须绑定该服务.

You can do it like this. One prerequisite to this method is, that your activity must bind the service.

首先,您要启动服务前台.

First you start service foreground.

private Notification mNotification;

public void onCreate() {
   ...
   startForeground(1, mNotification);
}

然后在您的活动中绑定和取消绑定服务,如下所示. BIND_ADJUST_WITH_ACTIVITY 对于在绑定到可见活动的时间内使服务保持活动状态很重要.

Then in your activity you bind and unbind the service as shown below. BIND_ADJUST_WITH_ACTIVITY is important for keeping service alive for the time it is bound to visible activity.

public void onStart() {
    ...
    Intent intent = new Intent(this, PlayerService.class);
    bindService(intent, mConnection, BIND_ADJUST_WITH_ACTIVITY);
}

public void onStop() {
    ...
    unbindService(mConnection);
}

现在这是过去的过去.当至少一个客户端连接到该服务时,您将停止前台;而当最后一个客户端断开连接时,您将启动前台.

Now here is the last past. You stop foreground, when at least one client is connected to the service, and you start foreground, when last client disconnects.

@Override
public void onRebind(Intent intent) {
    stopForeground(true); // <- remove notification
}

@Override
public IBinder onBind(Intent intent) {
    stopForeground(true); // <- remove notification
    return mBinder;
}

@Override
public boolean onUnbind(Intent intent) {
    startForeground(1, mNotification); // <- show notification again
    return true; // <- important to trigger future onRebind()
}

绑定服务时,必须考虑Android应用的规则.如果绑定了未启动的服务,则除非您在 BIND_ADJUST_WITH_ACTIVITY 标志之外指定了 BIND_AUTO_CREATE 标志,否则该服务将不会自动启动.

When binding a service, you have to consider rules applied by Android. If you bind a not started service, the service will not start automatically unless you specify BIND_AUTO_CREATE flag in addition to BIND_ADJUST_WITH_ACTIVITY flag.

    Intent intent = new Intent(this, PlayerService.class);
    bindService(intent, mConnection, BIND_AUTO_CREATE 
            | BIND_ADJUST_WITH_ACTIVITY);

如果在启动自动创建标记的情况下启动了服务,并且最后一个客户端解除了绑定,则服务将自动停止.如果要保持服务运行,则必须使用 startService()方法启动它.基本上,您的代码将如下所示.

If service was started with auto creation flag on, and last client unbinds then service will stop automatically. If you want to keep service running you have to start it with startService() method. Basically, your code will look like the one below.

    Intent intent = new Intent(this, PlayerService.class);
    startService(intent);
    bindService(intent, mConnection, BIND_ADJUST_WITH_ACTIVITY);

为已启动的服务调用 startService()对它没有影响,因为我们不会覆盖 onCommand()方法.

Calling startService() for already started service has no effect on it, as we do not override onCommand() method.

这篇关于活动可见时隐藏前台服务通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆