我是一个新的Android程序员,这是我的问题,我无法解决它。
我有3个类,MainActivity,Database和ProgressShow。
在类Database中,有一个复制资产中的大数据库的功能。在ProgressShow中,它用于构建ProgressDialog。
但是当我启动程序时,对话框没有显示,但它在大约几秒后确实停止了处理程序中的中断。似乎复制大文件时消息队列被卡住了。但我不知道为什么。这是我的计划,请帮助我。感谢。
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Database database = new Database(this);
database.CopyBigDatabase(CommonPara.DB_CONTENT_NAME,
CommonPara.DB_CONTENT_PATH + CommonPara.DB_CONTENT_NAME,
CommonPara.DB_CONTENT_COUNT);
}
}
public class Database
{
private Context mContext;
public Database(Context context)
{
mContext = context;
}
public SQLiteDatabase DbConnection(String file)
{
SQLiteDatabase db = null;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
db = SQLiteDatabase.openOrCreateDatabase(file, null);
}
else
{
}
return db;
}
public void CopyBigDatabase(final String name, final String dest, final int count)
{
new Thread()
{
public void run()
{
final ProgressShow dialog = new ProgressShow(
mContext, "please wait", "wait", ProgressShow.DIALOG_TYPE_BAR,
ProgressShow.DIALOG_DEFAULT_MAX);
dialog.ShowDialog();
try
{
InputStream is;
OutputStream os = new FileOutputStream(dest);
for (int i = 1; i <= count; i++)
{
is = mContext.getAssets().open(name + "." +
String.format("%03d", i));
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) > 0)
{
os.write(buffer, 0, length);
}
os.flush();
is.close();
if(dialog.GetProgress()
< ProgressShow.DIALOG_DEFAULT_MAX -1)
{
dialog.CloseDialog();
}
}
os.close();
}
catch (Exception e)
{
}
finally
{
dialog.CloseDialog();
}
}
}.run();
}
}
public class ProgressShow
{
private ProgressDialog dialog = null;
public static final int DIALOG_TYPE_SPINNER = 0;
public static final int DIALOG_TYPE_BAR = 1;
public static final int DIALOG_DEFAULT_MAX = 100;
public static final int DIALOG_DEFAULT_INCREASE = 1;
@SuppressLint("HandlerLeak")
Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
if(msg.what == 0)
{
dialog.show();
}
else
{
dialog.incrementProgressBy(msg.what);
if(GetProgress() >= GetMax())
{
dialog.cancel();
}
}
super.handleMessage(msg);
}
};
public ProgressShow(Context context, String title,
String message, int type, int max)
{
dialog = new ProgressDialog(context);
switch (type)
{
case 0:
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
break;
case 1:
default:
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
break;
}
dialog.setMax(max);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
dialog.setProgress(-dialog.getProgress());
}
public void ShowDialog()
{
handler.sendEmptyMessage(0);
}
public void AddProgress(int increase)
{
handler.sendEmptyMessage(increase);
int a = GetProgress();
}
public int GetProgress()
{
return dialog.getProgress();
}
public void CloseDialog()
{
handler.sendEmptyMessage(GetMax());
}
public int GetMax()
{
return dialog.getMax();
}
}
答案 0 :(得分:0)
必须在主线程中显示ProgressDialog;你试图从一个单独的线程中显示它,因为处理程序在创建它的线程中运行。
您的流程如下:您创建一个新线程,然后在此线程中创建一个新的ProgressShow。创建新的ProgressShow对象时,您将创建一个新的Handler对象。此处理程序在新线程中创建。无法从main以外的线程显示进度对话框。