我想在我的android應用程序中爲默認時間段創建一個計時器。 請幫我。我的應用程序正在計算一個班級的剩餘時間。如果課程在上午8:00開始,上午10:00結束,並且當前時間爲上午9:30時,計時器將顯示30分鐘。有誰能解決這個問題嗎?在android中添加默認計時器
0
A
回答
0
您可以使用處理程序和joda時間來計算您的分鐘數。首先是活動中的靜態內部類。靜態類可防止內存泄漏。該處理程序將每分鐘運行一次。
編輯:在活動傳遞將允許您調用其他非靜態方法在activty
public static class TimeLeftHandler extends Handler {
private String startTime;
private String endTime;
private WeakReference<MyActivity> activity
public TimeLeftHandler(MyActivity activity, String startTime, String endTime) {
this.startTime = startTime;
this.endTime = endTime;
this.activity = new WeakReference<MyActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
Log.d("Timeout", "Timeout thrown from SubjectSelection");
DateTime now = new DateTime();
DateTimeFormatter format = DateTimeFormat.forPattern("HH:mm");
LocalTime classStartTime = format.parseLocalTime(startTime);
LocalTime classEndTime = format.parseLocalTime(endTime);
DateTime nowRoundFormat = new DateTime().dayOfMonth().roundFloorCopy();
DateTime classStart = nowRoundFormat.plusHours(classStartTime.getHourOfDay()).plusMinutes(classStartTime.getMinuteOfHour());
DateTime classEnd = nowRoundFormat.plusHours(classEndTime.getHourOfDay()).plusMinutes(classEndTime.getMinuteOfHour());
if (now.isAfter(classStart) && now.isBefore(classEnd)) {
myMinutesTextView.setText("There are " + Minutes.minutesBetween(now, classEnd));
}
//call non-static method.
activity.get().nonstaticMethod();
sendEmptyMessageDelayed(0,60000);
}
那麼活動:
public class MyActivity extends Activity {
private TextView myMinutesTextView;
private TimeLeftHandler timeLeftHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
myMinutesTextView = (TextView) findViewById(R.id.mytextview);
timeLeftHandler = new TimeLeftHandler("08:00", "10:00");
}
@Override
protected void onStart() {
//start the handler
timeLeftHandler.sendEmptyMessage(0);
}
@Override
protected void onDestroy() {
//clean up.
timeLeftHandler.removeMessages(0);
}
private void nonstaticMethod() {
//Your code.
}
public static class TimeLeftHandler extends Handler {
.....
}
}
對於Android Studio中的gradle這個構建你的build.gradle文件:
dependencies {
compile 'joda-time:joda-time:2.4'
compile 'joda-time:joda-time:2.4'
compile 'joda-time:joda-time:2.2'
}
相關問題
- 1. 如何在android中的微調器中添加默認值?
- 2. 如何向EditText添加默認值:android
- 3. 在Android活動中添加計數器
- 4. CakePHP:在運行時向路由器添加默認值
- 5. 移除Couchbase在添加XDCR時添加的默認字段「doc」
- 6. Android:添加計時器使其崩潰
- 7. 在ggplot中添加默認標籤
- 8. 在IIS中添加默認路由
- 9. 在NetBeans中添加默認包的類
- 10. webview.loadurl在android默認瀏覽器中超時android
- 11. 添加時間默認值(7)字段
- 12. 在Android默認設置中添加應用程序設置
- 13. 如何在Android 4.0默認日曆中添加事件?
- 14. Android:在默認標題欄中添加搜索選項
- 15. 如何在AS3中添加計時器
- 16. 如何在as3中添加計時器
- 17. 在Java中添加時間計數器?
- 18. 在android中獲取默認時區
- 19. Datagrid添加行默認添加空行
- 20. 如何在Android應用程序中添加MX Player作爲默認播放器?
- 21. 如何在android中添加視頻計時器
- 22. 如何在activiti計時器事件中使用默認日曆?
- 23. JQGrid添加默認按鈕
- 24. AOSP - 添加默認Wifi SSID
- 25. 默認枚舉不添加
- 26. 默認添加NuGet包嗎?
- 27. 添加列的默認值
- 28. 添加默認值firebase
- 29. 添加默認列結果
- 30. 默認添加babel-polyfill?
所以基本上你需要不斷更新用戶界面,對嗎? – himanshu1496