问题描述
我的应用程序有一个回收站视图,该视图可以为每个特定项目启动带有通知和计时器的服务。当我启动计时器时,计时器将显示在设备的通知中。仅显示1个计时器时,此功能可以正常工作。但是,当运行多个计时器时,一些计时器会冻结并且不会倒计时。
我想知道是否可以在不冻结某些通知的情况下从该服务运行多个通知计时器。
在“回收者视图适配器”中选择某项时,在这里创建服务
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();
}
}
感谢您的帮助,谢谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)