应用程序适用于eclipse但不导出它(添加AsyncTask)

时间:2014-09-07 07:53:48

标签: android sqlite android-asynctask

我的应用程序使用SQLite DB显示信息列表(图片和数据)。我第一次午餐App,我不得不填充DB。如果我从Eclipse(没有调试模式但正常模式)午餐应用程序,这个过程可以正常工作,但如果我导出应用程序并从* .apk文件安装它,则无效(黑屏)。 我在午餐时更新数据库序列冻结应用程序。代码如下:

主要活动 菜单午餐文件选择器和查找* .csv文件的请求:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
    case R.id.updateDB:
        DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                case DialogInterface.BUTTON_POSITIVE:
                    //Yes button clicked
                    Intent intent1 = new Intent(MainActivity.this, FileChooser.class);
                    startActivityForResult(intent1,1);
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    //No button clicked
                    break;
                }
            }
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
            .setNegativeButton("No", dialogClickListener).show();
        return true;
    case R.id.action_settings:
        // add personal setting for load information into DB
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

选择一个侦听器catch文件并运行DatabaseHelper.SetUpDB

// Listen for results of FileChooser.
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    // See which child activity is calling us back.
    if (requestCode == 1){
        if (resultCode == RESULT_OK) { 
            String curFileName = data.getStringExtra("GetFileName");
            String curPath = data.getStringExtra("GetPath");
            if(curFileName.contains(".csv")){

                int screenOrientation = getResources().getConfiguration().orientation;
                this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
                (new DatabaseHelper(this)).setUpDB(new File(curPath + File.separatorChar + curFileName), 
                    new File(curPath + File.separatorChar + "IMG" + File.separatorChar));

                this.setRequestedOrientation(screenOrientation);
            }else{
                Toast.makeText(this, curFileName + " isn't a right scalebook", Toast.LENGTH_LONG).show();
            }
        }
     }
}

在这里你使用的是SetUpDB方法(和相关的)

public void setUpDB(File scaleBook, File IMG_Directory){
            try{
                appPath = scaleBook.getPath();
                appImgPath = IMG_Directory.getPath();
                if(scaleBook.exists() && IMG_Directory.exists()){ 
                    openDataBase(); // check if database is exists and if is not create one
                    (new populateIMGTable()).execute(appImgPath); // run a new thread
                }
            }catch(Error e){
                Toast.makeText(dbContext, "Error during SetUpDB", Toast.LENGTH_LONG).show();
        }
    }

class populateIMGTable extends AsyncTask<String, String, String> {
        ProgressDialog pDialog;
        PowerManager.WakeLock wl;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            PowerManager pm = (PowerManager) dbContext.getSystemService(
                    Context.POWER_SERVICE);
            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE,
                                    TAG);
            wl.acquire();
            pDialog = new ProgressDialog(dbContext);
            pDialog.setMessage("Loading Pictures...");
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setMax(100);
            pDialog.setProgress(0);
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

        }

        protected String doInBackground(String... args) {
            android.os.Debug.waitForDebugger(); // with this I can debug task too
            // import all images into DB
            return null;
        }

        protected void onProgressUpdate(Integer... values) 
        {
             pDialog.setProgress(values[0]);
        }

        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog after getting all products
            wl.release();
            pDialog.dismiss();
            (new importCSV()).execute(appPath); // run another task that import all info from *.csv file to DB
        }
    }

1 个答案:

答案 0 :(得分:0)

我发现了问题。为了调试我的任务,我添加了:

android.os.Debug.waitForDebugger();

这会在将文件导入数据库时​​冻结我的应用程序(它位于doInBackground任务方法中)。