Android 使用AsyncTask在Intent服务中维护SQLite数据库

Android 使用AsyncTask在Intent服务中维护SQLite数据库,android,sqlite,android-asynctask,android-service,intentservice,Android,Sqlite,Android Asynctask,Android Service,Intentservice,我已经实现了如下代码:向服务器发送新数据,并从服务器接收新数据 首先,接收新数据: 然后发送数据: 一切都进行得很顺利,但有时应用程序会因为数据库的原因而出现强制关闭错误 我想问题是在我同时查询数据库时发生的 时间服务也将调用数据库 在IntentService中维护数据库是否错误?还有别的办法吗 我可以不使用AsyncTask直接向服务器发送请求吗 非常感谢您的帮助。您应该在应用程序和服务中只使用一个dbHelper实例。在打开和执行关闭数据库时与synchronized stament协调。

我已经实现了如下代码:向服务器发送新数据,并从服务器接收新数据

首先,接收新数据: 然后发送数据: 一切都进行得很顺利,但有时应用程序会因为数据库的原因而出现强制关闭错误

我想问题是在我同时查询数据库时发生的 时间服务也将调用数据库

在IntentService中维护数据库是否错误?还有别的办法吗

我可以不使用AsyncTask直接向服务器发送请求吗


非常感谢您的帮助。

您应该在应用程序和服务中只使用一个dbHelper实例。在打开和执行关闭数据库时与synchronized stament协调。

如果发生崩溃,请从logcat发布堆栈跟踪。为什么在intent service中需要asynctask?在IntentService中不需要asynctask。onHandleIntent中的所有代码都将在后台线程上执行。Ok。我已经删除了AsyncTask,并在OnHandleContent中发布了整个代码。我的另一个问题是数据库将如何在服务中维护。请注意sqllite操作会阻塞UI线程。如果您计划将IntentService用作后台线程并保持UI的响应性,请注意在数据库上进行简短的操作…您能给我举个例子吗。
public class ReceivingOrderService extends IntentService {

    public ReceivingOrderService() {
    super("ReceivingDataService");
    // TODO Auto-generated constructor stub
    }

    /*
     * Context Variable
     */
    Context context;

    /*
     * Database Variable
     */
    DatabaseHelper dbHelper;


    /** 
     *  For Sending LastSyncTime to Server 
     **/
    String last_sync_time;

    @Override
    public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
    // TODO Auto-generated method stub
    /*
     * Getting Context Object
     */
    context = getApplicationContext();

    /* 
     * Getting Database Object and Opening DB
     */
    dbHelper = new DatabaseHelper(context);
    dbHelper.open();

    /*
     * Getting last_sync_time
     */
    last_sync_time = dbHelper.getLastSyncTime();

    if(last_sync_time == null)
    {
        this.stopSelf();
        last_sync_time = null;
    }

    new ReceiveDataTask().execute();
    }

    public class ReceiveDataTask extends AsyncTask<Void,Integer,JSONObject>
    {

        /***********
         *
         *
         * PROCESS for RECEIVING and UPDATING DB
         *
         *
         ***********/

        /**** DATABASE CLOSING *****/

        /**** starting Service for Sending New Data *****/
    }
}
public class SendingOrderService extends IntentService {
   new SendingDataTask().execute();
}

public class SendDataTask extends AsyncTask<Void,Integer,JSONObject>
{

}