冲刺day4

SplashActivity.java

package timeline.lizimumu.com.t.ui;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

import timeline.lizimumu.com.t.R;
import timeline.lizimumu.com.t.log.FileLogManager;

public class SplashActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        if (checkPermissions().size() == 0) {
            delayEnter();
        } else {
            requestPermissions();
        }
    }

    private void delayEnter() {
        new CountDownTimer(1200, 1200) {
            @Override
            public void onTick(long l) {
            }

            @Override
            public void onFinish() {
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
                finish();
            }
        }.start();
    }

    private List<String> checkPermissions() {
        List<String> permissions = new ArrayList<>();
        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.READ_PHONE_STATE);
        }
        return permissions;
    }

    private void requestPermissions() {
        List<String> permissions = checkPermissions();
        if (permissions.size() > 0) {
            ActivityCompat.requestPermissions(this, permissions.toArray(new String[permissions.size()]), 0);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            if (permission.equals(Manifest.permission.WRITE_EXTERNAL_STORAGE) && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                FileLogManager.init();
                break;
            }
        }
        delayEnter();
    }
}

SubTimerActivity.java

package timeline.lizimumu.com.t.ui;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Chronometer;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.fancy.androidutils.utils.DatePickerViewUtils;
import com.fancy.androidutils.utils.DateUtils;
import com.fancy.androidutils.utils.ToastUtils;

import java.util.Date;

import dev.xesam.android.toolbox.timer.CountDownTimer;
import timeline.lizimumu.com.t.R;
import timeline.lizimumu.com.t.data.TimerEntity;
import timeline.lizimumu.com.t.util.DateViewUtils;

public class SubTimerActivity extends AppCompatActivity {

    private TextView tvSettingTime;
    private TextView tvUsedTime;
    private boolean isFirst = true;
    private long startTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub_timer);


        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(getIntent().getStringExtra("title"));
        }

        initView();
        initData();
    }


    private void initView() {
        tvSettingTime = findViewById(R.id.tv_setting_time);
        tvUsedTime = findViewById(R.id.tv_used_time);

        tvSettingTime.setOnClickListener(view -> {
            if (isFirst) {
                DateViewUtils.getInstance().getAfterDateView(SubTimerActivity.this, (date, v) -> {
                    isFirst = false;
                    if (date.getTime() - System.currentTimeMillis() / 60 <= 0) {
                        Toast.makeText(SubTimerActivity.this, "时间已失效", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        tvSettingTime.setText(DateUtils.formatDateTime(date.getTime(), DateUtils.DF_HH_MM));
                        startTime = System.currentTimeMillis();

                        new CountDownTimer(date.getTime() - startTime, 1000) {

                            @Override
                            public void onTick(long millisUntilFinished) { // millisUntilFinished is the left time at *Running State*
                                Log.e("fanlcly", millisUntilFinished / 1000 + "");
                                int totalSec = (int) (millisUntilFinished / 1000);
                                Log.e("fanlcly", totalSec + "");
                                int min = totalSec / 60;
                                int hour = min / 60;

                                int hour_sec = totalSec % (60 * 60);
                                int sec = hour_sec % 60;

                                tvUsedTime.setText(hour + ":" + min + ":" + sec);
                            }

                            @Override
                            public void onCancel(long millisUntilFinished) {
                            }

                            @Override
                            public void onPause(long millisUntilFinished) {
                            }

                            @Override
                            public void onResume(long millisUntilFinished) {
                            }

                            @Override
                            public void onFinish() {
                                Toast.makeText(SubTimerActivity.this, "倒计时已结束", Toast.LENGTH_SHORT).show();
                                long endTime = System.currentTimeMillis();
                                Intent intent = new Intent();
                                TimerEntity timerEntity = new TimerEntity();
                                timerEntity.setStartTime(DateUtils.formatDateTime(startTime, DateUtils.DF_HH_MM));
                                timerEntity.setEndTime(DateUtils.formatDateTime(endTime, DateUtils.DF_HH_MM));

                                int totalSec = (int) ((endTime - startTime) / 1000);
                                Log.e("fanlcly", totalSec + "");
                                int min = totalSec / 60;
                                int hour = min / 60;
                                int hour_sec = totalSec % (60 * 60);
                                int sec = hour_sec % 60;

                                timerEntity.setTotalTime(hour + ":" + min + ":" + sec);
                                timerEntity.setEventName(getIntent().getStringExtra("title"));
                                intent.putExtra("entity", timerEntity);
                                setResult(2022, intent);
                                finish();
                            }
                        }.start();

                    }

                });
            }
        });


    }

    private void initData() {

    }


}

 

posted @ 2022-04-19 14:28  椅念琼  阅读(24)  评论(0编辑  收藏  举报