为什么Android数据库在onPause时插入错误

时间:2014-02-18 16:43:20

标签: android database android-asynctask

在我的Project android中,我有一个大数据,我想插入数据库 我正在使用AsyncTask将大数据插入数据库。平均需要插入5分钟。

但是当我的手机屏幕关闭时,存在两个错误。

1- for complete inserting the Screen must be On.  
2- if one or many time Screen will be off then database not good inserted.

Question

  
    

1-我错了。
    2-将大数据插入数据库的其他方法

  

My code

的一部分
public class MainActivity extends Activity {
    DatabaseHandler db;
    MobileInformation   mobile;
    private          SetDataBaseTask    setDataBaseTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        db = new DatabaseHandler(this);

        mobile = new MobileInformation();
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        String state = preferences.getString("IsSetDataBase","");
        if ( !state.equals("yes"))
        {
            setDataBaseTask=new setDataBaseTask();  
            setDataBaseTask.execute();
        }
    }

    public void firstDatabaseInitialize()
    {
            for (int i = 0;i < samsung0.brandStringList.length ; i++ )
            {
                mobile.Clean();
                mobile.Alert_types = samsung0.Alert_typesStringList[i];
                mobile.brand = samsung0.brandStringList[i];
                //and something other

                db.insertMobileInfo(mobile);
            }
                            //and many for for inserting
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        if (Loading==true)
        {
            stopLoadingAndDownloading();
            if (setDataBaseTask != null && setDataBaseTask.getStatus() != AsyncTask.Status.FINISHED)
                setDataBaseTask.cancel(true);
        }
        db.close();
    }

    private class SetDataBaseTask extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            showLoading();
        }

        @Override
        protected String doInBackground(Void... esult) {
            String msg="";
            db.open();
            db.clearTable("indexing");
            db.clearTable("mobile");
            firstDatabaseInitialize();
            db.close();
            return msg;
        }

        @Override
        protected void onPostExecute(String result) { 
            stopLoadingAndDownloading();    

             preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
             SharedPreferences.Editor editor = preferences.edit();
             editor.putString("IsSetDataBase","yes");
             editor.commit();
        }
    }
}

1 个答案:

答案 0 :(得分:2)

In my Project android i have a big data that i want insert to database.

然后你可以使用IntentService,当插入完成后发送广播,活动可以在该广播操作上注册(也需要取消注册),这样它就可以显示一些UI更新。

I am using AsyncTask for inserting big data to database. for average 5 min needed to inserting.

我认为这不是一个好主意,因为在方向改变时你会被搞砸!此外,您将在插入期间泄漏父活动。因此,将插入移动到IntentService会将插入与UI分离。