gpt4 book ai didi

android - 倒数计时器在后台更新用户界面

转载 作者:行者123 更新时间:2023-11-29 15:10:58 24 4
gpt4 key购买 nike

我有一个倒计时计时器,可以倒计时用户设置的当前时间。虽然,我希望能够访问其他应用程序,或者在运行倒计时计时器的同时转到设备的主屏幕,并继续使用倒计时剩余的计时器更新 TextView 。有什么办法吗?我尝试实现下面的服务,但我不知道这是否是完成此任务的正确方法,因为我确实收到了一个错误。

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



// if (savedInstanceState != null){
//
// }

titleTextView = (TextView)findViewById(R.id.titleTextView);
timeTextView = (TextView)findViewById(R.id.timeTextView);


Bundle getAlarmInfo = getIntent().getExtras();
titleOfAlarm = getAlarmInfo.getString("Title");
totalTime = getAlarmInfo.getString("totalTime");

number = new BigDecimal(totalTime).toBigInteger();


actualTimeFiniliazedInMilliSeconds = number.intValue();
titleTextView.setText(titleOfAlarm);

// countDown = new CountDownTimer(actualTimeFiniliazedInMilliSeconds, timeInterval);

// new UpdateCountDownTime().execute();


countDown = new CountDownTime(actualTimeFiniliazedInMilliSeconds, timeInterval);
countDown.start();
// startService(new Intent(getApplicationContext(), CountDown.class));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_count_down, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


public class CountDownTime extends CountDownTimer {


/**
* @param millisInFuture The number of millis in the future from the call
* to {@link #start()} until the countdown is done and {@link #onFinish()}
* is called.
* @param countDownInterval The interval along the way to receive
* {@link #onTick(long)} callbacks.
*/
public CountDownTime(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onTick(long millisUntilFinished) {



long millis = millisUntilFinished;
hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));


timeTextView.setText(hms);
}

@Override
public void onFinish() {
Intent goBack = new Intent(CountDownAct.this, ListOfAlarms.class);
startActivity(goBack);
finish();
}




}

@Override
protected void onPause() {
super.onPause();
timeTextView.setText(hms);





}



// public class CountDown extends Service{
//
// public CountDown () {
//
// }
//
// @Override
// public IBinder onBind(Intent intent) {
// return null;
// }
//
// @Override
// public void onCreate() {
// super.onCreate();
//
// countDown = new CountDownTime(actualTimeFiniliazedInMilliSeconds, timeInterval);
// countDown.start();
// }
// }

最佳答案

你需要使用 service 和 broadcast receiver 。我创建了一些 example for broadcast receiver给你。

检查一下,如果有问题请告诉我。

创建一个服务:TimerService.java

 @Override
public int onStartCommand(Intent intent, int flags, int startId) {
//Initialize and start the counter
countDown = new HokusFocusCountDownTimer(timeDifference, 1000);

countDown.start();

return super.onStartCommand(intent, flags, startId);
}

在 MainActivity.java 中

/*
* This is used to receive the updated time from broadcast receiver
* */
private final BroadcastReceiver timeBroadCaster = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//method used to update your GUI fields
updateGUI(intent);
}
};


private void updateGUI(Intent intent) {

AppLog.showLog("Update UI", "Timer Stopped");

if (intent.getExtras() != null) {

String focusHMS = intent.getStringExtra(Constants.HOCUS_FOCUS_TIMER);
focusMilliSecond = intent.getLongExtra(Constants.HOCUS_FOCUS_TIMER_MILLI_SECOND, 0);
txtFocusHourTimer.setText("" + focusHMS);
}
}


@Override
public void onResume() {
super.onResume();
registerReceiver(timeBroadCaster, new IntentFilter(
HokusFocusCountDownTimer.COUNTDOWN_BROADCAST_RECEIVER));


}

@Override
public void onPause() {
super.onPause();
try {
unregisterReceiver(timeBroadCaster);
} catch (Exception e) {
e.printStackTrace();
}

}

希望我的回答对你有所帮助

关于android - 倒数计时器在后台更新用户界面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31529021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com