2015-08-08 63 views
0

我正在使用ContentProvider來訪問數據庫並在我的Service類中構建通過HTTP發送數據的JSON請求。是否需要將內容提供者代碼放入線程

須藤代碼如下

Cursor cursor = this.getContentResolver().query(
      DB.EMPLOYEE_URI, 
      DB.EMPLOYEE.PROJECTION, 
      null, null, null); 

    while (cursor.moveToNext()) { 

     Employee empInfo= new Employee(); 

      int id = cursor.getInt(cursor 
        .getColumnIndex(DB.EMPLOYEE.COL._ID)); 
      String name= cursor.getString(cursor 
        .getColumnIndex(DB.EMPLOYEE.COL.EMPLOYEE_NAME)); 

     String reqJSON = getGSON() 
       .toJson(empInfo); 

    } 

那麼,我需要把它包起來了上面的代碼線程或異步任務的內部,使得它不會中斷主線程,因爲我不知道getContentResolver是非塊操作。

回答

0

不,但我會,如果你需要過濾,你不想攔截UI線程。只需使用ASyncTask,所以你不需要處理線程的生命週期。