okey我會嘗試根據我對您問題的理解給出一個解決方案: 您有一張包含照片的列表,並且您希望用戶有資格上傳這些圖片並更新其狀態的用戶狀態(已上傳/上傳/上傳/失敗),並且您不想在每次上傳時啓動Service
。
一個簡單可行的解決方案是使用IntnetService
我將只運行如果沒有分配給它的任務,將原子關閉時完成的工作,當然有IntentService
工作時的作業將在sperate線程
step 1
使數據庫表包含關於圖像的
_id integer
_image_uri
_image_status :
_image_status
數據將保持這些值中的一個(1-上傳:finish_uploaded,2-上傳:服務是上傳荷蘭國際集團的形象,3上傳:用戶可上傳圖片4失敗:未能現在上傳您可以重試)
step2
在UploadIntentService
嘗試將圖片上傳到服務器,當如果上傳成功競爭或發生錯誤時,如果你想上傳任何圖片上傳我們更新數據庫
public class UploadIntentService extends IntentService {
private static final String TAG = UploadIntentService.class.getSimpleName();
private static final int STATUS_UPLOAD = 0x01; //can be uploaded
public static final int STATUS_FAILED_TO_UPLOAD = 0x02; // tried to upload but failed
public static final int STATUS_UPLOADING = 0x03; // self explanied
public static final int STATUS_SUCCESSFULLY_UPLOADED = 0x04; // the image uploaded to server
public UploadIntentService() {
super(TAG);
}
@Override
protected void onHandleIntent(Intent intent) {
int status = intent.getIntExtra("status", -1);
String imageUri = intent.getStringExtra("image_path");
long imageDatabaseid = intent.getLongExtra("image_db_address",-1);
if(status != STATUS_SUCCESSFULLY_UPLOADED && status != STATUS_UPLOADING){
try{
//update _image_status column with value of STATUS_UPLOADING with the image_id = imageDatabaseid;
//upload code
//successfully uploaded
//update _image_status column with value of STATUS_SUCCESSFULLY_UPLOADED with the image_id = imageDatabaseid;
}catch(Exception ex){
ex.printStackTrace();
//update _image_status column with value of STATUS_FAILED_TO_UPLOAD with the image_id = imageDatabaseid;
}
}
}
}
......
step3
,你ListActivity
E本代碼
Intent intent = new Intent(context, UploadIntentService.class);
Bundle uploadExtras = new Bundle(3);
uploadExtras.putLong("image_db_address", PUT HERE THE IMAGE DATABASE ID);
uploadExtras.putInt("status", PUT HERE THE IMAGE STATUS);
uploadExtras.putString("image_path", PUT HERE THE IMAGE PATH IN FILE SYSTEM);
intent.putExtras(uploadExtras);
context.startService(intent);
....... 第4步
確保你聲明在manifest.xml服務,並享受。
謝謝大家,我正在測試IntentService和其他東西......需要更多的時間來實現這一點後,你的建議。 :-) – RRTW