我是一个新的android程序员,这是我无法解决的问题。
我有 3 个类,MainActivity、Database 和 ProgressShow。
在类数据库中,有一个功能可以复制资产中的大数据库。在 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();
}
}