Android 使用startForeground()调用为多个前台服务发出单一通知
我有一个有两种服务的应用程序 一个是使用Android 使用startForeground()调用为多个前台服务发出单一通知,android,android-service,android-notifications,Android,Android Service,Android Notifications,我有一个有两种服务的应用程序 一个是使用WindowManager在其他应用程序上显示浮动(覆盖)的UI。另一个是使用GooglePlayAPI进行位置跟踪。我的应用程序总是运行这些服务 我希望这些服务不会被操作系统杀死。所以我调用Service.startForeground()。但是,通知抽屉中有两个通知 是否有一种方法可以对两种服务使用单一通知?是的,这是可能的 如果我们查看Service.startForeground()签名,它接受通知id和通知本身()。因此,如果我们只想为多个前台服
WindowManager
在其他应用程序上显示浮动(覆盖)的UI。另一个是使用GooglePlayAPI
进行位置跟踪。我的应用程序总是运行这些服务
我希望这些服务不会被操作系统杀死。所以我调用Service.startForeground()
。但是,通知抽屉中有两个通知
是否有一种方法可以对两种服务使用单一通知?是的,这是可能的
如果我们查看Service.startForeground()签名,它接受通知id和通知本身()。因此,如果我们只想为多个前台服务提供一个通知,那么这些服务必须共享相同的通知id
我们可以使用singleton模式获得相同的通知和通知id。以下是示例实现:
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;
}
}
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;
}
}
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;
}
}
因此,我们可以在前台服务中使用这个类。例如,我们有MyFirstService.java和MySecondService.java:
MyFirstService.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;
}
}
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;
}
}
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;
}
}
MySecondService.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;
}
}
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;
}
}
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;
}
}
试着运行这些服务。瞧!您有一个针对多个前台服务的通知;) android中的两个通知意味着?@Pramod在服务中使用startForeground方法时,android系统会设置关于当前正在运行的服务的通知。我使用两种服务。并在这两个类中使用startForeground方法。一个选项是将这两个服务合并为一个服务。。我知道这不能回答问题。但如果两个服务同时启动和停止,这可能是您的一个选择。您找到解决此问题的方法了吗?我面临同样的问题…如果使用RemoteView定义通知布局,您仍然会遇到问题…我应该如何同时停止这两个服务的前台通知?@Ajay理想情况下,您可以让其他父类(如活动)启动这两个服务,并调用stopService()在适当的情况下(例如,活动的onDestroy,或单击活动视图上的按钮以停止所有服务)在这两个屏幕上显示。我希望这会有帮助。我们可以同时运行多少前台服务?