2017-02-10 30 views
0

嗯,我已經考慮過這個問題,我需要社區的幫助。Android在後臺執行ImageUpload並在回調中訪問意圖數據

我想上傳圖片(5張圖片)到當前在app的異步任務中完成的服務器。因此,當用戶關閉應用程序時可以停止上傳,所以我想使用IntentService執行此操作。

所以我創建了我的意圖服務來執行圖片上傳。

public class ImageUploadService extends IntentService { 


    /** 
    * Creates an IntentService. Invoked by your subclass's constructor. 
    * 
    * @param name Used to name the worker thread, important only for debugging. 
    */ 
    public ImageUploadService(String name) {super(name);} 

    public ImageUploadService(){ 
     this(ImageUploadService.class.getName()); 
    } 


    @Override 
    protected void onHandleIntent(Intent intent) { 
     //todo perform long running image upload operation 

     KbUserService kbUserService = JacksonRestRequestBuilder 
       .setupUploadRestService(RestUrls.BASE_URL_CUSTOMER, true, 
         KbUserService.class, getApplicationContext()); 

     LinkedHashMap<String, Object> requestBody = new LinkedHashMap<>(); 


     Call<LinkedHashMap<String,Object>> call = kbUserService.uploadFile(requestBody); 

     call.enqueue(new KbCallback() { 
      @Override 
      public void failure(Map<String, Object> serverErrorResponse, String genericErrorMessage) { 
       //todo send notification that image upload failed and also send broadcast event that image upload failed. 
      } 

      @Override 
      public void success(Map<String, Object> successResponse) { 

       Map<String, Object> modelDataFromResponseAsMap = 
         ClientModelUtils.getModelDataFromResponseAsMap(successResponse); 
//     Log.i(NAME,"Background Image upload completed for "+msg.arg1); 
       String fileUrl = ClientModelUtils.getString(modelDataFromResponseAsMap, 
         ModelConstants.UserConstants.IMAGE_URL); 
       //todo send notification that image upload success and send broadcast that image upload success. 
//     uploadFileSuccess(fileUrl, profName); 
//     bean.setUploadInProgress(false); 
//     view.uploadFileSuccess(fileUrl,profName); 
      } 

      @Override 
      public void networkOrUnexpectedError(String message) { 
       //todo send notification that image upload failed and also send broadcast event that image upload failed. 
      } 
     }); 

    } 

    protected void showToast(final String msg){ 
     //gets the main thread 
     Handler handler = new Handler(Looper.getMainLooper()); 
     handler.post(new Runnable() { 
      @Override 
      public void run() { 
       // run this code in the main thread 
       Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 


} 

所以我在這裏發送廣播成功響應所有註冊的活動。

我不能夠解決以下問題,

1.Suppose時出來的應用程序,並回到活動,怎麼知道什麼是當前上傳圖片?

2.How訪問意圖數據onHandleIntent()內回調?因爲意圖數據我有,所以當請求成功,我想與imageUrlimageType發送廣播有關imageType信息。

回答

0

這可能不是理想的答案,但我通常在服務和活動上都有廣播接收器。這樣他們可以相互溝通。例如,當活動再次開始時,我會向通知它的服務發送廣播。該服務將隨後發送廣播及其信息。

public class SomeService extends Service { 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     registerReceiver(receiver, new IntentFilter(SOME_SERVICE_ACTIONS)); 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 

     unregisterReceiver(receiver); 
    } 

    private BroadcastReceiver receiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      switch (intent.getIntExtra(SERVICE_COMMAND, 0)) { 
       case ACTIVITY_STARTED: 
        sendCallback(); 

       break; 
     } 
    }; 

} 
+0

如何識別當前正在執行的任務?還有如何訪問回調中的意圖數據? – Manikanta

+0

您應該將您的意圖數據保存爲一個字段。至於目前的上傳任務,你將不得不在你正在使用的庫上查看。如果您可以從中獲得回撥,您將能夠廣播該信息。 –