Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 运行服务时发生数据库锁定错误_Android_Service - Fatal编程技术网

Android 运行服务时发生数据库锁定错误

Android 运行服务时发生数据库锁定错误,android,service,Android,Service,我有一个服务,在服务的帮助下,每10分钟用数据更新一次Sqllite数据库。我已经打开了一个数据库连接,并在数据更新时关闭了连接 在我的应用程序中,我有不同的数据库更新页面。我还打开了一个数据库连接并关闭了每个页面的连接以进行数据更新 问题是,当服务运行时,当连接在服务中打开时,我无法通过应用程序将数据更新到sqlite 是否有任何方法可以使用服务和应用程序同时运行数据库更新 有人帮我拿样品吗 我在我的服务中调用一个函数,如下代码所示 public class service_helper ex

我有一个服务,在服务的帮助下,每10分钟用数据更新一次Sqllite数据库。我已经打开了一个数据库连接,并在数据更新时关闭了连接

在我的应用程序中,我有不同的数据库更新页面。我还打开了一个数据库连接并关闭了每个页面的连接以进行数据更新

问题是,当服务运行时,当连接在服务中打开时,我无法通过应用程序将数据更新到sqlite

是否有任何方法可以使用服务和应用程序同时运行数据库更新

有人帮我拿样品吗

我在我的服务中调用一个函数,如下代码所示

public class service_helper extends Service {
    public static final String TAG = "Service";
    private NotificationManager mNM;
    private DatabaseAdapter dbAdapter;
    private service_updator serviceUpdator;
    private int NOTIFICATION = 1;
    private Timer timer = new Timer();
    @Override
    public IBinder onBind(Intent arg0) {

        return null;

    }

    @Override
    public void onCreate() {

        super.onCreate();


        serviceUpdator = new service_updator(this);
        dbAdapter = new DatabaseAdapter(this);
        dbAdapter.open();
        Toast.makeText(this, "Service created ...", Toast.LENGTH_LONG).show();   
        startService() ;
    }

    @Override
    public void onStart(Intent intent, int startid) {

    }

    @Override
    public void onDestroy() {
        timer.cancel();
        super.onDestroy();

        Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();

    }

     private void startService()     
     {                   
         timer.scheduleAtFixedRate(new mainTask(), 0, 250000);   
         }    
     private class mainTask extends TimerTask    
     {         
         public void run()    
         {           
             try {
                serviceUpdator.UploadData();
                Log.d(TAG, "Service");
            } catch (ParserConfigurationException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
             }    
         } 



    private Runnable threadBody = new Runnable() {


             public void run() {

            try {

                serviceUpdator.UploadData();

                Log.d(TAG, "Service");
            } catch (ParserConfigurationException e) {
                            }

        }




    };


    }

UploadData() Function Code

    dbAdapter = new DatabaseAdapter(this.context);
            dbAdapter.open();
            Long transactionType = null;
            String transactionData = null;
            String sql = "Select * from tblTransaction where PKTransaction >?";
            Cursor cursorTransaction = dbAdapter.ExecuteRawQuery(sql, "-1");
            cursorTransaction.moveToFirst();
            dbAdapter.close();

                  for (int i = 0; i < cursorTransaction.getCount(); i++) {
                  }

    dbAdapter.close();

Application Code

    private void SaveSortOrder() throws Exception {
            try {
                String server1IPAddress = "";
                String server2IPAddress = "";
                String deviceId = "";

                Cursor cursorTransaction;
                Cursor cursorAdmin;

                DatabaseAdapter dbAdapter;
                DataXmlExporter dataXmlExporter;
                admin_helper adminhelper;
                Date date = new Date();

                SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
                String RevisedEstimatedDate = sdf.format(date);

                adminhelper = new admin_helper(this);
                cursorAdmin = adminhelper.GetAdminDetails();
                if (cursorAdmin.moveToFirst())
                    server1IPAddress = cursorAdmin.getString(cursorAdmin
                            .getColumnIndex("RemoteServer1IPAddress"));
                server2IPAddress = cursorAdmin.getString(cursorAdmin
                        .getColumnIndex("RemoteServer2IPAddress"));
                deviceId = cursorAdmin.getString(cursorAdmin
                        .getColumnIndex("DeviceID"));
                cursorAdmin.close();

                ContentValues initialSortOrder = new ContentValues();
                ContentValues initialTransaction = new ContentValues();
                for (int i = 0; i < ListSortOrder.getAdapter().getCount(); i++) {

                    HashMap result = (HashMap) ListSortOrder.getItemAtPosition(i);
                    View vListSortOrder;
                    vListSortOrder = ListSortOrder.getChildAt(i);

                    TextView Sort_DeliveryOrder = (TextView) vListSortOrder
                            .findViewById(R.id.et_Sort_Order);

                    initialSortOrder.put("DeliveryOrder", Sort_DeliveryOrder
                            .getText().toString());
                    dbAdapter = new DatabaseAdapter(this);
                    dbAdapter.open();

                    dbAdapter.BeginTransaction();
                    dbAdapter.UpdateRecord("tblDelivery", initialSortOrder,
                            "PKDelivery" + "="
                                    + result.get("Sort_PKDelivery").toString(),
                            null);

                    dataXmlExporter = new DataXmlExporter(this);
                    dataXmlExporter.StartDataSet();

                    String sqlTransaction = "Select 5 as TransactionType,'Update Delivery Order' as Description,'"
                            + result.get("Sort_PKDelivery").toString()
                            + "' as FKDelivery, "
                            + " deviceId as DeviceID ,'"
                            + Sort_DeliveryOrder.getText().toString()
                            + "' as DeliveryOrder ,date() as TransactionUploadDate,time() as TransactionUploadTime from tblAdmin where PKAdmin > ?";

                    cursorTransaction = dbAdapter.ExecuteRawQuery(sqlTransaction,
                            "-1");
                    dataXmlExporter.AddRowandColumns(cursorTransaction,
                            "Transaction");
                    String XMLTransactionData = dataXmlExporter.EndDataSet();

                    try {

                        if ((server1IPAddress != "") && (server2IPAddress != "")) {
                            try {
                                if (server1IPAddress != "") {
                                    InsertUploadedTrancasctionDetails(
                                            server1IPAddress, deviceId,
                                            XMLTransactionData);
                                }
                            } catch (Exception exception) {

                                if ((server1IPAddress != server2IPAddress)
                                        && (server2IPAddress != "")) {
                                    InsertUploadedTrancasctionDetails(
                                            server2IPAddress, deviceId,
                                            XMLTransactionData);
                                }
                            }

                        }
                    } catch (Exception exception) {

                        initialTransaction
                                .put("ReceivedDate", RevisedEstimatedDate);
                        initialTransaction.put("TransactionData",
                                XMLTransactionData);

                        dbAdapter.InsertRecord("tblTransaction", "",
                                initialTransaction);

                    }

                    dbAdapter.SetSucessfulTransaction();
                    dbAdapter.EndTransaction();
                    dbAdapter.close();

                }
            } catch (Exception exception) {
                throw exception;
            }
        }

我在迁移到Honeycomb时遇到了类似的问题,它开始抛出SQLiteDatabaseLockedException,我通过在我的数据库上放置一个内容提供商对其进行排序,然后Android处理所有线程问题。当然,如果您愿意,您可以尝试查找线程问题,但这肯定会解决它:

我有一个类似的应用程序,其服务每5分钟运行一次,我没有问题。您的SQLiteHelper类应该能够处理各种请求,而不会出现问题。你能把密码贴出来吗?