带有服务和通知的计时器

时间:2019-04-12 10:28:13

标签: java android service android-notifications

具有以下任务: 编写最简单的计时器应用程序。

支持的功能:

-启动计时器

-暂停计时器

重置计时器

要求:

-计时器不应说谎;)就是为了显示错误的时间

-如果计时器正在运行并且应用程序已最小化,请显示计时器继续运行的警报。

-警报应支持以下选项: 暂停/恢复计时器按钮/计时器重置按钮

-通过单击警报,用户必须返回到应用程序 计时器应该继续工作,直到用户明确终止计时器(重置计时器)

-如果尚未重置计时器,则在重启手机后,应用程序应继续工作

-该应用程序应在所有版本的Android OS上都能正常运行。 (尤其是在Android O及更高版本上)

问题是 我们启动计时器->打开屏幕->返回初始位置->我们将计时器设置为暂停状态->计时器中的计时器与应用程序中的计时器不同步

所以,我的代码

先谢谢您

公共类TimerService扩展服务{

private static final String TIMER_VALUE_TAG = "TIMER_VALUE";
private static final int TIMER_NOTIFICATION_ID = 1;

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public boolean stopService(Intent name) {
    return super.stopService(name);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    String timerValue = intent.getStringExtra(TIMER_VALUE_TAG);

    Intent mainActivityIntent = new Intent(this, MainActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, 0);

    Notification notification = new NotificationCompat.Builder(this,TIMER_TAG)
            .setContentTitle("Timer")
            .setContentText(timerValue)
            .setSmallIcon(R.drawable.ic_notification_timer)
            .setContentIntent(pendingIntent)
            .build();
    startForeground(TIMER_NOTIFICATION_ID, notification);

    return START_REDELIVER_INTENT;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

}

公共类MainActivity扩展了AppCompatActivity实现的View.OnClickListener {

private static final long START_TIME_IN_MILLIS = 600000;
private static final String SHARED_PREFERENCE_TAG = "TIMER_PREFERENCE";
private static final String TIME_MILLIS_LEFT_TAG = "TIME_MILLIS_LEFT";
private static final String TIMER_RUNNING_TAG = "TIMER_RUNNING";
private static final String END_TIMER_TAG = "END_TIMER";
private long mTimeLeftInMillis;
private long endTimer;
private TextView timerView;
private static final String TIMER_VALUE_TAG = "TIMER_VALUE";
private CountDownTimer countDownTimer;
private boolean timerRunning;
private Button startStopButton;
private Button resetButton;
private Intent timerIntent;



@Override
protected void onStop() {
    super.onStop();
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFERENCE_TAG, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putLong(TIME_MILLIS_LEFT_TAG, mTimeLeftInMillis);
    editor.putLong(END_TIMER_TAG, endTimer);
    editor.putBoolean(TIMER_RUNNING_TAG, timerRunning);
    editor.apply();
    stopTimerService();
}

@Override
protected void onStart() {
    super.onStart();
    SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFERENCE_TAG, MODE_PRIVATE);
    mTimeLeftInMillis = sharedPreferences.getLong(TIME_MILLIS_LEFT_TAG, START_TIME_IN_MILLIS);
    timerRunning = sharedPreferences.getBoolean(TIMER_RUNNING_TAG, false);
    updateTimer();
    updateButtons();
    if(timerRunning) {
        endTimer = sharedPreferences.getLong(END_TIMER_TAG, 0);
        mTimeLeftInMillis = endTimer - System.currentTimeMillis();
        if(mTimeLeftInMillis < 0) {
            mTimeLeftInMillis = 0;
            timerRunning = false;
            updateTimer();
            updateButtons();
        }
        else {
            startTimer();
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timerView = findViewById(R.id.timer);
    resetButton = findViewById(R.id.btn_reset);
    startStopButton = findViewById(R.id.btn_start_stop);
    timerIntent = new Intent(this, TimerService.class);
    startStopButton.setOnClickListener(this);
    resetButton.setOnClickListener(this);
}

private void startTimerService() {
    String timerValue = timerView.getText().toString();
    timerIntent.putExtra(TIMER_VALUE_TAG, timerValue);
    ContextCompat.startForegroundService(this, timerIntent);
}

private void stopTimerService() {
    stopService(timerIntent);
}


@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.btn_reset:
            resetTimer();
            break;
        case R.id.btn_start_stop:
            if(timerRunning) {
                stopTimer();
            }
            else {
                startTimer();
            }
            break;
    }
}

private void stopTimer() {
    countDownTimer.cancel();
    timerRunning = false;
    updateButtons();
}

private void startTimer() {
    endTimer = System.currentTimeMillis() + mTimeLeftInMillis;
    countDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            mTimeLeftInMillis = millisUntilFinished;
            updateTimer();
            startTimerService();
        }

        @Override
        public void onFinish() {
            timerRunning = false;
            updateButtons();
        }
    }.start();
    timerRunning = true;
    updateButtons();
}

private void updateTimer() {
    int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
    int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
    String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
    timerView.setText(timeLeftFormatted);
}

private void resetTimer() {
    timerRunning = false;
    mTimeLeftInMillis = START_TIME_IN_MILLIS;
    stopTimerService();
    updateTimer();
}

private void updateButtons() {
    if(timerRunning) {
        resetButton.setVisibility(View.GONE);
        startStopButton.setText(R.string.timer_pause);
    }
    else {
        startStopButton.setText(R.string.button_start);
        if(mTimeLeftInMillis < 1000) {
            resetButton.setVisibility(View.GONE);
        }
        else {
            startStopButton.setVisibility(View.VISIBLE);
        }
        if(mTimeLeftInMillis < START_TIME_IN_MILLIS) {
            resetButton.setVisibility(View.VISIBLE);
        }
        else {
            resetButton.setVisibility(View.GONE);
        }
    }
}

}

0 个答案:

没有答案