Android 使用线程显示进度条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10782344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
displaying progressbar using threading
提问by Kumar
I am trying to display a progress bar using threading .. I accept that I do not have that much concept of threading.
我正在尝试使用线程显示进度条.. 我接受我没有那么多线程的概念。
Here is the code
这是代码
public class Progress extends Activity {
static String[] display;
private static final int Progress = 0;
private ProgressBar mProgress;
private int mProgressStatus = 0;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
mProgress = (ProgressBar) findViewById(R.id.progressBar);
// Start lengthy operation in a background thread
new Thread(new Runnable() {
public void run() {
while (mProgressStatus < 100) {
mProgressStatus = doWork();
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
mProgress.setProgress(mProgressStatus);
}
});
}
}
private int doWork() {
display = new Logic().finaldata();
// TODO Auto-generated method stub
return 100;
}
}).start();
}
}
On running, the logcat message is
在运行时,logcat 消息是
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
java.lang.RuntimeException:无法在尚未调用 Looper.prepare() 的线程内创建处理程序
What is the mistake that I am doing here?
我在这里做的错误是什么?
回答by Simon Dorociak
So your problem will be elsewhere. I tried your example with Handler and it works for me.
所以你的问题将在别处。我用 Handler 试过你的例子,它对我有用。
package com.sajmon.threadsDemo;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ThreadsDemoActivity extends Activity {
ProgressBar bar;
TextView label;
Handler handler = new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bar = (ProgressBar) findViewById(R.id.progBar);
new Thread(new Runnable() {
int i = 0;
int progressStatus = 0;
public void run() {
while (progressStatus < 100) {
progressStatus += doWork();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
handler.post(new Runnable() {
public void run() {
bar.setProgress(progressStatus);
i++;
}
});
}
}
private int doWork() {
return i * 3;
}
}).start();
}
}
And XML:
和 XML:
<ProgressBar
android:id="@+id/progBar" style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
So look at this and edit your code similar with this.
所以看看这个并编辑与此类似的代码。
回答by Sri
Find the below example code for progress bar update using threads
找到以下使用线程更新进度条的示例代码
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ThreadDemo1ProgressBar extends Activity
{
ProgressBar bar;
TextView msgWorking;
boolean isRunning = false;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
bar.incrementProgressBy(5);
if (bar.getProgress() == bar.getMax()) {
msgWorking.setText("Done");
bar.setVisibility(View.INVISIBLE);
} else {
msgWorking.setText("Working..." +
bar.getProgress());
}
}// handleMessage
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
bar = (ProgressBar) findViewById(R.id.progress);
bar.setMax(100);
msgWorking = (TextView) findViewById(R.id.TextView01);
}
public void onStart() {
super.onStart();
bar.setProgress(0);
Thread background = new Thread(new Runnable() {
public void run() {
try
{
for (int i = 0; i < 20 && isRunning; i++) {
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
}
catch(Throwable t) {
// just end the background thread
}
}
});
isRunning = true;
background.start();
}// onStart
public void onStop() {
super.onStop();
isRunning = false;
}
}// ThreadDemo1ProgressBar
The about example updating the progress bar for every 5 seconds.
关于每 5 秒更新一次进度条的示例。
回答by Swaroop
I actually just create a thread instance once and it works anyway. This code was written in the Startup Activity. All you need to do is call showSpinner1()
method to show/hide the spinner.
我实际上只是创建了一个线程实例,无论如何它都可以工作。此代码是在启动活动中编写的。您需要做的就是调用showSpinner1()
方法来显示/隐藏微调器。
Ensure to do this
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
in your onCreate()
method and use this code for toggling the spinner ON and OFF.
确保getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
在您的onCreate()
方法中执行此操作
并使用此代码打开和关闭微调器。
// Spinner related code - The thread is created just once and is used multiple times (works!!)
boolean toShow = false;
Thread spinner1Thread = new Thread("Show/Hide Spinner Thread") {
@Override
public void run() {
setProgressBarIndeterminateVisibility(toShow);
}
};
/**
* Shows and hides the spinner
* @param pShow
*/
public void showSpinner1(boolean pShow) {
toShow = pShow;
runOnUiThread(spinner1Thread);
}