Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid ProgressDialog multi-thread dialog doesn't appear
    primarykey
    data
    text
    <p>i'm a new android programmer, here's my question that i can't solve it.</p> <p>i have 3 classes, MainActivity, Database and ProgressShow.</p> <p>in class Database, there's a function to copy a big database in assets. and in ProgressShow, it's used to build a ProgressDialog.</p> <p>but when i start the program, the dialog didn't show, but it did stoped at the break in handler after about several seconds. it seems that the message queue was stucked when copying the big file. but i don't know why. and here's my program, please help me. thanks.</p> <pre><code>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 &lt;= count; i++) { is = mContext.getAssets().open(name + "." + String.format("%03d", i)); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) &gt; 0) { os.write(buffer, 0, length); } os.flush(); is.close(); if(dialog.GetProgress() &lt; 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() &gt;= 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(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload