从 recyclerView 启动服务

Start service from recyclerView

我的任务是当用户点击切换器时调用 RecyclerView 中的服务。我的服务是 CountDownTimer 在后台工作。而且我不知道如何从我的持有人那里呼叫服务。

我有错误:

Non-static method 'startServiceTimer()' cannot be referenced from a static contex

我需要在我的服务中启用 multiply countDownTimer,你能帮我吗?

我的持有人

    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();
                        }
                    }
                });

MainActivity 上的 startServiceTimer

protected void startServiceTimer(){
        startService(new Intent(this, BroadcastService.class));
        Log.d("BroadcastService", "Started service");
    }

我的服务随机

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;
    }
}

不好意思这个问题,我才学的:)

你应该在这里实现监听器模式。您的 activity 将监听来自 recyclerview 的 'startTimer' 事件。

执行以下操作:

首先,声明接口:

public interface TimerStarter {
    void startTimer();
}

将此接口传递给您的 RecyclerViewAdapter 和 ViewHolder 构造函数并将其保存到局部变量。

public class MyRvAdapter extends RecyclerViewAdapter<SiteHolder> {
    private TimerStarter starter;
    public MyRvAdapter(TimerStarter timerStarter) {
        starter = timerStarter;
    }

    // Your code
    // Do not forget to pass TimerStarter to SiteHolder via consturctor
}

class SiteHolder extends RecyclerView.ViewHolder implements 
    View.OnClickListener, View.OnLongClickListener{

    ... // Your fields

    private TimerStarter starter;

    SiteHolder(View itemView, TimerStarter timerStarter) {
        starter = timerStarter;
    }

    // You can call starter.startTimer() where nesessary
 }

接下来,在你的activity

中实现这个接口
public class MainActivity extends Activity implements TimerStarter {
  ... // Your methods here

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ... // Instructions
        // this = TimerStarter
        RecyclerViewAdapter adapter = new YourAdapter(this)
        ... // etc
    }

    @Override 
    public void startServiceTimer(){ // Make method public
        startService(new Intent(this, BroadcastService.class));
        Log.d("BroadcastService", "Started service");
    }

  ... // Other methods
}