=========================================
3.18程序加载中,请稍候
ProgressDialog与线程整合使用.
效果图:
代码:
package edu.cqutzx.ProgramOnloading;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
publicclass ProgramOnLoadingActivity extends Activity {
private Button btn;
private TextView tv;
public ProgressDialog pd = null;
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Find the object by ID.*/
btn = (Button) findViewById(R.id.btn);
tv = (TextView) findViewById(R.id.tv);
/* Add a listener to the Button (btn) */
btn.setOnClickListener(new Button.OnClickListener ()
{
@Override
publicvoid onClick(View v)
{
/* Find the Strings in file strings.xml */
final CharSequence strDialogTitle = getString(R.string.strDialogTitle);
final CharSequence strDialogBody = getString(R.string.strDialogBody);
/*Show Progreess Dialog. */
/*It's ok to write like this :*/
pd = ProgressDialog.show(ProgramOnLoadingActivity.this,strDialogTitle, strDialogBody);
//pd = ProgressDialog.show(ProgramOnLoadingActivity.this,strDialogTitle, strDialogBody,true);
tv.setText(strDialogBody);
new Thread()
{
@Override
publicvoid run() {
try
{
//System.out.println("aaaaaa");
sleep(10000);
//System.out.println("bbbbbb");
//Toast.makeText(getBaseContext(), strDialogBody, Toast.LENGTH_LONG).show();
//System.out.println("ccccc");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
/* Let the dialog disappear .*/
pd.dismiss();
/*Mobile tv had used this method: when EIXT the program, show a Dialog first
* and then kill the progrom.*/
//finish();
}
}
}.start();
}
}
);
}
}
问题:
Can't create handler inside thread that has not called Looper.prepare()
解决办法:
http://mycoding.iteye.com/blog/882756
总结:线程的使用
new Thread()
{
Public void run()
{
Tasktodo();
}
}