建议使用 SDK26.
https://github.com/zhangqifan1/Demo_ok0
本来我他么一堆想法,后来用countdown挺顺手,嗯,真香.
主要是在弹出窗和Activity之间各种切换就行
直接代码了 真相帝
1.App
public class App extends Application {
//主要是用来记录切换到哪个分钟数了
private static int clickpositon = 0;
public static int getClickpositon() {
return clickpositon;
}
public static void setClickpositon(int clickpositon) {
App.clickpositon = clickpositon;
}
}
Manifest .name App
2.弹窗我用的这个
https://github.com/H07000223/FlycoDialog_Master
compile 'com.flyco.dialog:FlycoDialog_Lib:1.3.2@aar'
implementation 'com.android.support:design:28.0.0'
//RecyclerView 快速适配器
compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
package com.as.demo_ok0;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import com.as.demo_ok0.databinding.DialogCountdownBinding;
import com.flyco.animation.Attention.Swing;
import com.flyco.dialog.widget.base.BaseDialog;
import java.util.List;
public class CustomBaseDialog extends BaseDialog {
private Adapter adapter;
private List list;
public void setList(List list) {
this.list = list;
}
public CustomBaseDialog(Context context) {
super(context);
}
public Adapter getAdapter() {
return adapter;
}
@Override
public View onCreateView() {
widthScale(0.85f);
showAnim(new Swing());
View inflate = View.inflate(mContext, R.layout.dialog_countdown, null);
DialogCountdownBinding bind = DataBindingUtil.bind(inflate);
adapter = new Adapter(R.layout.item_countdown, list);
bind.recyclerView.setAdapter(adapter);
bind.recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
return inflate;
}
@Override
public void setUiBeforShow() {
}
}
3.Adapter
public class Adapter extends BaseQuickAdapter {
public Adapter(int layoutResId, @Nullable List data) {
super(layoutResId, data);
}
@Override
protected void convert(BaseViewHolder helper, CountDownBean item) {
View view1 = helper.getView(R.id.rela);
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = mContext.obtainStyledAttributes(attrs);
int backgroundResource = typedArray.getResourceId(0, 0);
view1.setBackgroundResource(backgroundResource);
int time = item.getTime();
if(time==0){
helper.setText(R.id.tvText, "不开启");
}else{
helper.setText(R.id.tvText, time + "分钟");
}
RadioButton butSelect = helper.getView(R.id.butSelect);
butSelect.setChecked(item.isSelect());
helper.addOnClickListener(R.id.butSelect);
}
}
4.Bean
public class CountDownBean {
private int time;
private boolean isSelect;
public int getTime() {
return time;
}
public void setTime(int time) {
this.time = time;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
public CountDownBean(int time, boolean isSelect) {
this.time = time;
this.isSelect = isSelect;
}
}
5.MainAc
public class MainActivity extends AppCompatActivity {
private CountDownTimer countDownTimer;
public List list;
private TextView tvTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tvDialog = findViewById(R.id.tvDialog);
tvTime = findViewById(R.id.tvTime);
CountDownBean countDownBean0 = new CountDownBean(0, false);
CountDownBean countDownBean1 = new CountDownBean(1, false);
CountDownBean countDownBean2 = new CountDownBean(2, false);
CountDownBean countDownBean3 = new CountDownBean(3, false);
CountDownBean countDownBean4 = new CountDownBean(4, false);
CountDownBean countDownBean5 = new CountDownBean(5, false);
list = new ArrayList<>();
list.add(countDownBean0);
list.add(countDownBean1);
list.add(countDownBean2);
list.add(countDownBean3);
list.add(countDownBean4);
list.add(countDownBean5);
list.get(App.getClickpositon()).setSelect(true);
final CustomBaseDialog customBaseDialog = new CustomBaseDialog(this);
customBaseDialog.setList(list);
int time = list.get(App.getClickpositon()).getTime();
//实例化
if (time != 0) {
countDownTimer = new CountDownTimer(time * 60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tvTime.setText(millisUntilFinished / 1000 + "秒");
}
@Override
public void onFinish() {
Toast.makeText(MainActivity.this, "完成倒计时", Toast.LENGTH_SHORT).show();
tvTime.setText("完成倒计时");
}
}.start();
}
tvDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customBaseDialog.show();
Adapter adapter = customBaseDialog.getAdapter();
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
click(adapter,position,customBaseDialog);
}
});
adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
click(adapter,position,customBaseDialog);
}
});
}
});
}
public void click(BaseQuickAdapter adapter,int position,CustomBaseDialog customBaseDialog){
int timelast = list.get(App.getClickpositon()).getTime();
App.setClickpositon(position);
for (int i = 0; i < list.size(); i++) {
list.get(i).setSelect(false);
}
list.get(position).setSelect(true);
adapter.notifyDataSetChanged();
customBaseDialog.setList(list);
customBaseDialog.dismiss();
int time1 = list.get(position).getTime();
if (time1 == 0) {
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
}
tvTime.setText("显示时间");
} else if (time1 == timelast) {//和当前设置的是同一个 那么不用动
return;
} else {
if (countDownTimer != null) {
countDownTimer.cancel();
countDownTimer = null;
}
countDownTimer = new CountDownTimer(time1 * 60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tvTime.setText(millisUntilFinished / 1000 + "秒");
}
@Override
public void onFinish() {
Toast.makeText(MainActivity.this, "完成倒计时", Toast.LENGTH_SHORT).show();
tvTime.setText("完成倒计时");
}
}.start();
}
}
}
6.xml item
7.dialog
8.Main
9.然而现在这个只是离开界面就不行的那种,所以要服务,Service,然而需要试试获取秒数所以需要传值,能实现这种传值的广播
....我用的EventBus
public class MainActivity extends BaseActivity {
public List list;
private TextView tvTime;
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
@Override
protected void getBundleExtras(Bundle extras) {
}
@Override
protected void initView() {
TextView tvDialog = findViewById(R.id.tvDialog);
tvTime = findViewById(R.id.tvTime);
CountDownBean countDownBean0 = new CountDownBean(0, false);
CountDownBean countDownBean1 = new CountDownBean(1, false);
CountDownBean countDownBean2 = new CountDownBean(2, false);
CountDownBean countDownBean3 = new CountDownBean(3, false);
CountDownBean countDownBean4 = new CountDownBean(4, false);
CountDownBean countDownBean5 = new CountDownBean(5, false);
list = new ArrayList<>();
list.add(countDownBean0);
list.add(countDownBean1);
list.add(countDownBean2);
list.add(countDownBean3);
list.add(countDownBean4);
list.add(countDownBean5);
list.get(App.getClickpositon()).setSelect(true);
final CustomBaseDialog customBaseDialog = new CustomBaseDialog(this);
customBaseDialog.setList(list);
int time = list.get(App.getClickpositon()).getTime();
//实例化
if (time != 0 && !TimeService.isstarting) {
starttime(time);
}
tvDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
customBaseDialog.show();
Adapter adapter = customBaseDialog.getAdapter();
adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
@Override
public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
click(adapter, position, customBaseDialog);
}
});
adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
@Override
public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
click(adapter, position, customBaseDialog);
}
});
}
});
}
private void starttime(int time) {
Intent intent = new Intent(this, TimeService.class);
intent.setAction(TimeService.ACTION_START);
intent.putExtra("time", time);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
}
@Override
protected void initData() {
}
@Override
protected void initListener() {
}
public void click(BaseQuickAdapter adapter, int position, CustomBaseDialog customBaseDialog) {
int timelast = list.get(App.getClickpositon()).getTime();
App.setClickpositon(position);
for (int i = 0; i < list.size(); i++) {
list.get(i).setSelect(false);
}
list.get(position).setSelect(true);
adapter.notifyDataSetChanged();
customBaseDialog.setList(list);
customBaseDialog.dismiss();
int time1 = list.get(position).getTime();
if (time1 == 0) {
stoptime();
} else if (time1 == timelast) {//和当前设置的是同一个 那么不用动
return;
} else {
starttime(time1);
}
}
private void stoptime() {
Intent intent = new Intent(this, TimeService.class);
intent.setAction(TimeService.ACTION_STOP);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent);
} else {
startService(intent);
}
tvTime.setText("显示时间");
}
@Override
public boolean useEventBus() {
return true;
}
@Subscribe(sticky = true)
public void onReceiveEvent(EventTimeBean eventTimeBean) {
int type = eventTimeBean.getType();
switch (type) {
case 0:
long millisUntilFinished = eventTimeBean.getTickBean().getMillisUntilFinished();
tvTime.setText(millisUntilFinished / 1000 + "秒");
break;
case 1:
tvTime.setText("完成倒计时");
break;
default:
break;
}
}
}
10.Service
public class TimeService extends Service {
public static final String ACTION_START = "com.play.ACTION_START";
public static final String ACTION_STOP = "com.play.ACTION_STOP";
private CountDownTimer countDownTimer;
public static boolean isstarting=false;
@Override
public void onCreate() {
super.onCreate();
startForeground(1, new Notification());
}
@Override
public void onDestroy() {
super.onDestroy();
stopForeground(true);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (TextUtils.equals(action, ACTION_START)) {
isstarting=true;
int time = intent.getIntExtra("time", 0);
//todo 每次的time才会发来start的Action 所以这里每次让他重新new
if (countDownTimer != null)
countDownTimer.cancel();
countDownTimer = null;
countDownTimer = new CountDownTimer(time * 60 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
EventBus.getDefault().postSticky(new EventTimeBean(0,new TickBean(millisUntilFinished)));
}
@Override
public void onFinish() {
EventBus.getDefault().postSticky(new EventTimeBean(1));
}
}.start();
} else if (TextUtils.equals(action, ACTION_STOP)) {
isstarting=false;
if (countDownTimer != null)
countDownTimer.cancel();
countDownTimer = null;
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
OK