简体   繁体   English

Android:对多个通知使用一项服务 class?

[英]Android : Use one service class for multiple notifications?

I have an app with multiple users, each user can start multiple counters for each activity they do ( run, sleep, reading...) to count how many seconds passed.我有一个有多个用户的应用程序,每个用户可以为他们所做的每个活动(运行、睡眠、阅读......)启动多个计数器来计算经过了多少秒。 Each counter will show time passed with a notification.每个计数器都会通过通知显示经过的时间。 This is sample code这是示例代码

Service class:服务 class:

public class ExampleService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        //String CHANNEL_ID="abc123";
        String input = intent.getStringExtra("inputExtra");
        Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,
                0, notificationIntent, 0);
        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("Example Service")
                .setContentText(input)
                .setSmallIcon(R.drawable.icon_play)
                .setContentIntent(pendingIntent)
                .build();
        startForeground(100, notification);
        //do heavy work on a background thread
        //stopSelf();
        return START_STICKY;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

Main activity: Call startService() to start the service and the notification Call stopService() to stop the service and the notification主要活动:调用 startService() 来启动服务和通知 调用 stopService() 来停止服务和通知

public void startService(View v) {
    String input = editTextInput.getText().toString();
    Intent serviceIntent = new Intent(this, ExampleService.class);
    serviceIntent.putExtra("inputExtra", input);
    ContextCompat.startForegroundService(this, serviceIntent);
}
public void stopService(View v) {
    Intent serviceIntent = new Intent(this, ExampleService.class);
    stopService(serviceIntent);
}

Each notification work independently, the user can stop one activity by clicking on the stop button on a notification while the other notifications are still running.每个通知独立工作,用户可以通过单击通知上的停止按钮来停止一个活动,而其他通知仍在运行。

If I use one Service class for each activity, it works, but it is impossible to add more service classes when I add a new user.如果我为每个活动使用一个服务 class,它可以工作,但是当我添加新用户时,不可能添加更多服务类。 ** **

If I use only one Service class for all notifications, when I call stopService(), all the notifications will be destroyed.如果我只对所有通知使用一个服务 class,当我调用 stopService() 时,所有通知都将被销毁。

** **

Is there any solution to use ONLY ONE Service class for ALL NOTIFICATIONS and users can control each notification INDEPENDENTLY???是否有任何解决方案仅对所有通知使用一项服务 class 并且用户可以独立控制每个通知???

Don't stop the service.不要停止服务。 Bind to it and send it a message to stop a specific notification.绑定到它并向其发送消息以停止特定通知。 The service should then stop only the notification that it was requested to end.然后服务应该只停止请求结束的通知。 The service can end itself when there are no notifications left to stop.当没有通知要停止时,服务可以自行结束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM