我有一个服务,它将在服务的帮助下每10分钟用数据更新Sqllite数据库。我已打开数据库连接并在数据更新时关闭连接。
在我的应用程序中,我有不同的页面进行数据库更新。我还打开了数据库连接并关闭了每个页面的连接以进行数据更新。
问题是,当服务正在运行时,我无法在服务中打开conncetion时通过我的应用程序将数据更新为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;
}
}
答案 0 :(得分:0)
我在迁移到Honeycomb时遇到了类似的问题,它开始抛出SQLiteDatabaseLockedException我通过将内容提供程序放在我的数据库上对其进行排序,然后Android处理所有线程问题。如果你想要的话,你可以尝试找到线程问题,但这肯定会解决它:)