My task is a call service in the RecyclerView when user click on switcher. My service is CountDownTimer work on background. And I dont know how call service from my holder.
I have error:
Non-static method 'startServiceTimer()' cannot be referenced from a static contex
And I need to enable multiply countDownTimer`s in my service, can you help me with it?
My Holder
class SiteHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
CardView cardView;
TextView sitename;
ImageView sitePhoto;
SwitchCompat switchNotify;
TextView textDate;
LinearLayout linearCard;
CountDownTimer t;
SiteHolder(View itemView) {
super(itemView);
cardView = itemView.findViewById(R.id.cardView);
sitename = itemView.findViewById(R.id.site_name);
sitePhoto = itemView.findViewById(R.id.site_photo);
switchNotify = itemView.findViewById(R.id.notificationSwitcher);
textDate = itemView.findViewById(R.id.textDate);
linearCard = itemView.findViewById(R.id.linearCard);
switchNotify.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (b){
t = new CountDownTimer(finish, tick) {...}.start(); //my local timer in recycler view
; MainActivity.startServiceTimer();
} else {
switchOff();
t.cancel();
}
}
});
startServiceTimer on MainActivity
protected void startServiceTimer(){
startService(new Intent(this, BroadcastService.class));
Log.d("BroadcastService", "Started service");
}
and my Service with random
public class BroadcastService extends Service {
private final static String TAG = "BroadcastService";
private final static String COUNTDOWN_BR = "package.countdown_br";
Intent bi = new Intent(COUNTDOWN_BR);
CountDownTimer cdt = null;
private int cnt_service = 0;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Starting service timer...");
cnt_service++;
Log.d(TAG, "Count service = " + cnt_service);
cdt = new CountDownTimer(30000, 1000) {
@Override
public void onTick(long l) {
Log.d(TAG, "Countdown timer seconds reamaning: " + l/1000);
bi.putExtra("countdown", l);
sendBroadcast(bi);
}
@Override
public void onFinish() {
Log.d(TAG, "FIRST timer finished.");
}
}.start();
}
@Override
public void onDestroy() {
cdt.cancel();
Log.d(TAG, "Timer cancelled");
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
cnt_service++;
Log.d(TAG, "Calling onStart command");
Log.d(TAG, "Count service = " + cnt_service);
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Sorry for this question, I am only learn this:)