java—服务上运行的多个通知

r8uurelv  于 2021-07-04  发布在  Java
关注(0)|答案(0)|浏览(229)

我的应用程序有一个回收器视图,它可以启动一个服务,为每个特定项目提供通知和计时器。当我启动计时器时,计时器将显示在设备上的通知中。当只显示一个计时器时,此功能正常工作。但是,当运行多个计时器时,有些计时器将冻结而不倒计时。
我想知道是否有可能从这个服务运行多个通知计时器而不冻结一些通知。
这里是我在recycler视图适配器中选择项目时创建服务的地方

holder.img_start.setOnClickListener(new View.OnClickListener() {
                @RequiresApi(api = Build.VERSION_CODES.O)
                @Override
                public void onClick(View view) {
                    holder.img_start.setImageResource(R.drawable.ic_stop);
                    holder.img_start.setClickable(false);

                    Intent intent = new Intent(context, TimerService.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("name", timer.getTimer_name());
                    bundle.putString("img", timer.getTimer_img());
                    bundle.putInt("dur", 10000);
                    bundle.putInt("id", timer.getTimer_id());
                    intent.putExtra("bundle", bundle);
                    context.startForegroundService(intent);
                }
            });

以下是创建的计时器服务:

package com.ashb.osrstimers;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.IBinder;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;

public class TimerService extends Service {

    private Bundle bundle;
    private String timer_img;
    private String timer_name;
    private int timer_id;
    private int duration;
    private int icon;
    private Context context = this;

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);

        this.bundle = intent.getBundleExtra("bundle");
        this.timer_img = bundle.getString("img");
        this.timer_name = bundle.getString("name");
        this.timer_id = bundle.getInt("id");
        this.duration = bundle.getInt("dur");

        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "notifyLemubit")
                .setSmallIcon(this.icon)
                .setContentTitle("Timer Running")
                .setContentText("Time Until Your " + this.timer_name + " Tree has Fully Grown: " + this.duration)
                .setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(this.timer_id, builder.build());

        new CountDownTimer(10 * 1000, 1000) {

            @Override
            public void onTick(long ms_until_done) {
                builder.setContentText("Time Until Your " + timer_name + " Tree has Fully Grown: " + ms_until_done / 1000);
                notificationManagerCompat.notify(timer_id, builder.build());
            }

            @Override
            public void onFinish() {
                notificationManagerCompat.cancel(timer_id);
                final NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
                        .setSmallIcon(icon)
                        .setContentTitle("Timer Finished")
                        .setContentText("Your " + timer_name + " is Fully Grown!")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

                final NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
                notificationManagerCompat.notify(timer_id, builder.build());
            }
        }.start();

        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
}

谢谢你的帮助

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题