Thread.sleep() की गारंटी नहीं है। इसका मतलब यह है कि इस अवधि के लिए विषय के बाहर होने वाले विभिन्न कारणों से आप उस अवधि के लिए सो सकते हैं या नहीं सो सकते हैं।
आप तो आप शायद पर लैंड करेगा "एंड्रॉयड में टाइमर" के लिए शुद्ध खोज करते हैं इन दो: https://developer.android.com/reference/java/util/Timer.html और https://developer.android.com/reference/java/util/concurrent/ScheduledThreadPoolExecutor.html
आप उन्हें बाहर की जाँच, हालांकि, मैं उन का उपयोग नहीं होगा सकता है के रूप में वे अन्य का एक बहुत प्रदान करते हैं नाम के रूप में कार्यशीलता का सुझाव है ("अनुसूचित थ्रेडपूलएक्सएटर")। आपको इसकी आवश्यकता नहीं है क्योंकि बहुत सारे धागे आदि के साथ बड़ी प्रणालियों के लिए इसका उपयोग होने की संभावना है ...
यदि मैं सही ढंग से आपका कोड समझता हूं तो आप प्रगति पट्टी को अपडेट करने का प्रयास कर रहे हैं। जो आप करने की कोशिश कर रहे हैं उसके लिए मैं एक हैंडलर का उपयोग करने का सुझाव दूंगा।हैंडलर का मुख्य उपयोगों में से एक संदेशों शेड्यूल करने के लिए है और डॉक्स यहाँ के रूप में विनिर्दिष्ट भविष्य में किसी समय के रूप में क्रियान्वित किया जाना runnables: http://developer.android.com/reference/android/os/Handler.html
मैं इस तरह यह करना होगा:
int counter = 0;
int delayInMs = 5000; // 5 seconds
Handler timer = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
counter++;
// If the counter is mod 5 (or whatever) lower the delay
if (counter % 5 == 0) {
delayInMs/=100; // Or any other calculation.
}
// If the counter reaches 100 the counting will not continue.
if (counter <= 100) {
// If the counter has not reached the condition to stop, the handler
// will call the timer again with the modified (or not) delay.
timer.sendEmptyMessageDelayed(0, delayInMs);
// Change progress
updateProgress(counter);
}
return true;
}
});
// ...
// Somwhere in the code to start the counter
timer.sendEmptyMessageDelayed(0, delayInMs); // starts the timer with the initial 5 sec delay.
एक और चीज़। उस लाइन में जहां आपके पास यह कोड है:
progress.setProgress(t);
अन्य धागे से यूआई तत्वों को कॉल करते समय सावधान रहें, यह सिरदर्द का स्रोत है। यदि आपका हैंडलर किसी अन्य थ्रेड में है तो आपको उस कॉल को फ़ंक्शन में लपेटना चाहिए और सुनिश्चित करें कि इसे मुख्य थ्रेड (यानी यूआई थ्रेड) से बुलाया जाता है। इसे प्राप्त करने के कई तरीके (हमेशा आवश्यक नहीं)। उनमें से एक इस तरह है:
private void updateProgress(int counter) {
WhateverActivity.this.runOnUiThread(new Runnable() {
public void run() {
progress.setProgress(counter);
}
});
}
स्रोत
2015-06-23 13:01:24
क्या आप अधिक कोड दिखा सकते हैं? आप टाइमर शुरू करते हैं? – Wishmaster