0
我試圖在App小部件中顯示不確定的進度條2秒(作爲數據刷新的指示)。但小部件沒有顯示出來。我嘗試使用Handler.postdelayed和Thread.sleep。兩種方法都不起作用。小部件根本沒有顯示出來。Android小工具:進度條不可見
在部件佈局
<ProgressBar
style="?android:attr/progressBarStyleLargeInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/widget_progress_bar"
android:layout_centerInParent="true"
android:indeterminate="false" />
我即使StyleHorizontal審判欄樣式。
在的AppWidgetProvider類
日上午在上updateAppWidget開始調用它()方法:
方法1:新的線程
public static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
final RemoteViews widgetView = new RemoteViews(context.getPackageName(), R.layout.widget_wifi_info_basic);
new Thread(new Runnable() {
int progress = 0;
Handler progressHandler = new Handler();
public void run() {
long timerEnd = System.currentTimeMillis() + 3 * 1000;
while (timerEnd > System.currentTimeMillis()) {
progress = (int) (timerEnd - System.currentTimeMillis())/300;
// Update the progress bar
progressHandler.post(new Runnable() {
public void run() {
widgetView.setProgressBar(R.id.widget_progress_bar,3000,progress,false);
Log.d("tag", "progress=" +progress);
}
});
try {
Thread.sleep(300);
} catch (InterruptedException e) {
Log.w("tag","Progress thread cannot sleep");
}
}
}
}).start();
...
...
}
方法2 :CountdownTimer(再次在同一個方法中)
widgetView.setViewVisibility(R.id.widget_progress_bar,View.VISIBLE);
new CountDownTimer(2000, 100) {
@Override
public void onTick(long millisUntilFinished) {
//this will be done every 1000 milliseconds (1 seconds)
int progress = (2000 - (int)millisUntilFinished)/100;
widgetView.setProgressBar(R.id.widget_progress_bar,2000,progress,false);
Log.d(Constants.APP_NAME, "progress=" +progress);
}
@Override
public void onFinish() {
//the progressBar will be invisible after 2 seconds
widgetView.setViewVisibility(R.id.widget_progress_bar,View.INVISIBLE);
}
}.start();
方法3:嘗試這對於不確定的進度條;休眠線程
// sleep for few seconds
try {
// Show progress bar
widgetView.setViewVisibility(R.id.widget_progress_bar, View.VISIBLE);
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.w("tag","Progress thread cannot sleep");
}
// Hide progress bar
widgetView.setViewVisibility(R.id.widget_progress_bar, View.INVISIBLE);
令人驚訝的日誌消息(用於打印進度)永遠不會被印!?