我有计时器代码,但如果我更改片段,计时器会重置为 00:00 我希望该计时器在我单击停止或暂停后仍然计数,或者这意味着该计时器仍然在后台计数
怎么做 ?
这是我的代码,
public class TimerFragment extends BaseFragment {
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_timer, container, false);
initialize();
//when start
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
//pause
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
});
return rootView;
}
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
secs = secs % 60;
timerValue.setText("" + mins + ":"
+ String.format("%02d", secs));
customHandler.postDelayed(this, 0);
}
};
不管怎样,谢谢你
问题 :-当您切换片段并返回计时器片段时,将调用 onCreate 生命周期回调,并重置您的计时器
customHandler.postDelayed(updateTimerThread, 0);
解决方案 :-将计时器逻辑移至 Activity 类,但活动将在方向更改时重新创建,因此将计时器逻辑移至 Application 类:-
将此类放入您的项目包中
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Application;
import android.os.Handler;
import android.widget.Toast;
public class App extends Application {
public static App appInstance;
private SimpleDateFormat dateFormat;
@Override
public void onCreate() {
super.onCreate();
appInstance = this;
dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
}
public void afficher() {
Toast.makeText(getBaseContext(), dateFormat.format(new Date()), 300).show();
handler.postDelayed(runnable,1000);
}
public void startTimer() {
runnable.run();
}
public void stopTimer() {
handler.removeCallbacks(runnable);
}
Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
afficher();
}
};
}
设置你的应用名称 to 应用类像下面这样(应用程序的 android 名称属性):-
<application
android:name="com.masterdetail_webview.App"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.masterdetail_webview.TimertestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
现在您可以像这样从任何地方启动和停止计时器
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
App.appInstance.startTimer();
}
});
findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
App.appInstance.stopTimer();
}
});
添加应用程序控制器
![enter image description here](https://i.stack.imgur.com/UzWtP.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)