当用户点击切换台时,我的任务是RecyclerView中的呼叫服务。我的服务是CountDownTimer在后台工作。而且我不知道我的持有人如何拨打电话服务。
我有错误:
非静态方法' startServiceTimer()'不能从a引用 静态上下文
我需要在我的服务中启用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;
}
}
对不起这个问题,我只是学习这个:)
答案 0 :(得分:0)
你应该在这里实现Listener模式。您的活动将聆听“startTimer”和“#start;来自recyclerview的活动。
请执行以下操作:
首先,声明接口:
public interface TimerStarter {
void startTimer();
}
将此界面传递给您的RecyclerViewAdapter& ViewHolder consturctors并将其保存到局部变量。
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
}
接下来,在您的活动中实施此界面
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
}