2015-08-08 61 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,所以你不需要处理线程的生命周期。