从同时运行的服务访问数据库

时间:2014-03-17 15:54:04

标签: android android-sqlite

我正在开发一个从服务器获取数据的应用程序,这些数据需要长时间运行,所以我使用3个服务。 数据包含文本(需要存储在数据库中)和图像(存储在sdCard中) 问题是当我同时运行3个服务时,我的许多数据都没有插入数据库中。 我在每个服务中创建了一个数据库实例,所以我可以访问它。 这是我的DatabaseHandler类

public class DatabaseHandler {
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;

private Context context;

public DatabaseHandler (Context c){
    context = c;

}

public DatabaseHandler openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}

public DatabaseHandler openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();

return this;
}

public void close(){
    if(sqLiteHelper != null)
    {
        sqLiteHelper.close();
    }
}
// queries

public class SQLiteHelper extends SQLiteOpenHelper {

public SQLiteHelper(Context context, String name,
  CursorFactory factory, int version) {
 super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
 // TODO Auto-generated method stub
 db.execSQL(CREATE_RESTAURANTS);
 db.execSQL(CREATE_BRANCHES);
 db.execSQL(CREATE_MENUS);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
}

}

我的服务代码:

public class DownloadBranches extends IntentService{

public static final String NOTIFICATION = "com.example.aaasasa";
NotificationManager mNotificationManager;
private DatabaseHandler myDatabaseHandler;

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

@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
    myDatabaseHandler = new DatabaseHandler(this);
    myDatabaseHandler.openToWrite();
    try {
        if(!myDatabaseHandler.checkDatabaseIsNull(DatabaseHandler.MYDATABASE_TABLE_BRANCHES))
        {
            getUpdatedBranches();
        }
        if(myDatabaseHandler.checkDatabaseIsNull(DatabaseHandler.MYDATABASE_TABLE_BRANCHES))
        {
            getBranches();
        }
    }
    catch (Exception e )
    {   
    }
//      myDatabaseHandler.close();
    Intent i = new Intent(NOTIFICATION);
//      sendBroadcast(i);
}

public void getData()
{
//      myDatabaseHandler.openToWrite();
    try {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setSoTimeout(params, 0);
        HttpClient httpClient = new DefaultHttpClient(params);

        //prepare the HTTP GET call 
        HttpGet httpget = new HttpGet("http://restaurants.bugs3.com/branches.php");
        //get the response entity
        HttpEntity entity = httpClient.execute(httpget).getEntity();

        if (entity != null) {
            //get the response content as a string
            String response = EntityUtils.toString(entity);
            //consume the entity
            entity.consumeContent();

            // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources
            httpClient.getConnectionManager().shutdown();

            //return the JSON response
            JSONObject object = new JSONObject(response.trim());
            JSONArray jsonArray = object.getJSONArray("branches");
            if(jsonArray != null) {
               for(int i = 0 ; i < jsonArray.length() ; i++) {

                   JSONObject object1 = (JSONObject) jsonArray.get(i); 
                   int server_branch_id = object1.getInt("server_branch_id");
                   String branch_region = object1.getString("branch_region");
                   String branch_name = object1.getString("branch_name");
                   double branch_lat = object1.getDouble("branch_lat");
                   double branch_lon = object1.getDouble("branch_lon");
                   double distance = object1.getDouble("distance");
                   String restaurant_name = object1.getString("restaurant_name");
                   myDatabaseHandler.insertBranchWithID(server_branch_id, branch_region, branch_name, restaurant_name, branch_lat, branch_lon, distance);   
               }
            } 
        }
    }catch (Exception e) {
        e.printStackTrace();
    }
    sendNotification("Branches Downloaded");
}

0 个答案:

没有答案