在数据库中保存数据时,Android AlertDialog会冻结

时间:2016-08-10 10:32:09

标签: android android-alertdialog

在我的应用中,我在异步中使用AlertDialog。但是,当数据在数据库中保存时,它会冻结。我该怎么做才能让它继续运行?它可以运行一段时间,但在访问数据库的某段时间后会停止。

这是我的代码:

class BackGroundTasks extends AsyncTask<String, String, Void> {
@Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        if (dialog == null) {
            dialog = ProgressDialog.show(mActivity, null,
                    "Please wait ...", true);

        }

    }
    @Override
    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub

        CheckInternetConnection internet = new CheckInternetConnection(
                mActivity);
        if (!internet.HaveNetworkConnection()) {
            return null;
        }
        return null;
    }
    protected void onPostExecute(Void result) {

        super.onPostExecute(result);
        try {
            CheckInternetConnection internet = new CheckInternetConnection(
                    getApplicationContext());
            if (!internet.HaveNetworkConnection()) {
                showToast("No Internet Connection.");
                return;
            } else {
         setUpdatedBarcodes();
           }
           }
           }
           }

        private boolean setUpdatedBarcodes(
            ArrayList<Model_BarcodeDetail> changedBarcodeList2) {

        try {
            int i = 0;
            BarcodeDatabase barcodeDatabase = new  
            BarcodeDatabase(mActivity);
            barcodeDatabase.open();
            for (Model_BarcodeDetail model : changedBarcodeList2) {
                barcodeDatabase.updateEntry(model, userId);
            }

            barcodeDatabase.close();
            if (RefList1.equals(RefList)) {

                if (dialog != null) {
                    dialog.dismiss();  // cancelling Async dialog here after  
                                          data is saved in DB
                }
                showToast("Barcodes updated successfully");
            }
        } catch (Exception e) {
            Log.i("Exception caught in: ", "setDownloadedBarcodes method");
            e.printStackTrace();
            return false;
        }
        return true;

    }

3 个答案:

答案 0 :(得分:0)

DB操作应该在后台线程中完成。也可以将它放在doInBackground()方法中。

答案 1 :(得分:0)

在数据库中保存数据时,不要在主线程上执行此操作在后台线程上执行。试试代码

new Handler().postDelayed(new Runnable() {
     @Override
     public void run() {
         // do your work
     }
},0);

new Thread(new Runnable() {
    public void run() {
         // do your work here
    }
}).start();

答案 2 :(得分:0)

我修改你的代码。可能有帮助......

class BackGroundTasks extends AsyncTask<String, String, Void> {
@Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        if (dialog == null) {
            dialog = ProgressDialog.show(mActivity, null,
                    "Please wait ...", true);
        }
    }
    @Override
    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub

        CheckInternetConnection internet = new CheckInternetConnection(
                mActivity);
        if (!internet.HaveNetworkConnection()) {
            showToast("No Internet Connection.");
        } else {
            setUpdatedBarcodes();
        }
        return null;
    }
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (dialog != null) {
             dialog.dismiss();  // cancelling Async dialog here
        }
    }
}

private boolean setUpdatedBarcodes(
     ArrayList<Model_BarcodeDetail> changedBarcodeList2) {
        try {
            int i = 0;
            BarcodeDatabase barcodeDatabase = new  
            BarcodeDatabase(mActivity);
            barcodeDatabase.open();
            for (Model_BarcodeDetail model : changedBarcodeList2) {
                barcodeDatabase.updateEntry(model, userId);
            }

            barcodeDatabase.close();
            if (RefList1.equals(RefList)) {
                showToast("Barcodes updated successfully");
            }
        } catch (Exception e) {
            Log.i("Exception caught in: ", "setDownloadedBarcodes method");
            e.printStackTrace();
            return false;
        }
        return true;
    }