本文介绍了使用startForeground()调用多个前景服务的单一通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有两项服务的应用程序.

I have an app that has two services.

一个用于显示使用WindowManager在其他应用上浮动(覆盖)的UI.另一个用于使用GooglePlayAPI进行位置跟踪.我的应用程序始终运行这些服务.

One is for showing UI for floating (overlay) on other apps using WindowManager. The other is for Location Tracking using GooglePlayAPI. My app always runs these services.

我希望这些服务不被操作系统杀死.所以我叫Service.startForeground().但是,通知抽屉中有两个通知.

I want these services not to be killed by the OS. So I call Service.startForeground(). However there are two notifications in the notification drawer.

是否可以对两种服务都使用一个通知?

Is there a way to use a single notification for both services?

推荐答案

是的,有可能.

如果我们查看Service.startForeground()签名,它会同时接受通知ID和&通知本身(参阅文档).因此,如果我们只想为一个以上的前台服务提供单个通知,则这些服务必须共享相同的通知和通知.通知ID.

If we take a look at Service.startForeground() signature, it accept both notification id & the notification itself (see documentation). Hence, if we want to have an only single notification for more than one foreground services, these services must share the same notification & notification id.

我们可以使用单例模式来获取相同的通知&通知ID.这是示例实现:

We can use the singleton pattern to get the same notification & notification id. Here is the example implementation:

NotificationCreator.java

public class NotificationCreator {

    private static final int NOTIFICATION_ID = 1094;
    private static final String CHANNEL_ID = "Foreground Service Channel";
    private static Notification notification;

    public static Notification getNotification(Context context) {

        if(notification == null) {

            notification = new NotificationCompat.Builder(context, CHANNEL_ID)
                    .setContentTitle("Try Foreground Service")
                    .setContentText("Yuhu..., I'm trying foreground service")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .build();
        }

        return notification;
    }

    public static int getNotificationId() {
        return NOTIFICATION_ID;
    }
}

因此,我们可以在前台服务中使用此类.例如,我们有MyFirstService.java& MySecondService.java:

Thus, we can use this class in our foreground services. For example, we have MyFirstService.java & MySecondService.java:

MyFirstService.java

public class MyFirstService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(NotificationCreator.getNotificationId(),
                NotificationCreator.getNotification(this));
    }

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

MySecondService.java

public class MySecondService extends Service {

    @Override
    public void onCreate() {
        super.onCreate();
        startForeground(NotificationCreator.getNotificationId(),
                NotificationCreator.getNotification(this));
    }

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

只需尝试运行这些服务.瞧!您只有一个通知,可以提供多个前台服务;)!

Just try to run these services. Voila! You have a single notification for multiple foreground services ;)!

这篇关于使用startForeground()调用多个前景服务的单一通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:12