2017-02-10 1 views
0

この問題を考えているので、コミュニティの助けが必要です。AndroidはバックグラウンドでImageUploadを実行し、コールバックでインテントデータにアクセスします

現在、アプリケーションの非同期タスクで行われているサーバーにイメージ(5イメージ)をアップロードします。したがって、ユーザーがアプリを閉じるときにアップロードを停止することができますので、この操作は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次のような問題を解決することはできませんよ?リクエストが成功する場合、コールバック内onHandleIntent()に意図データにアクセスするための

2.How ?,意図データに私はimageTypeについての情報を持っているのでので、私はimageUrlimageTypeでブロードキャストを送信したいです。

答えて

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

インテントデータはフィールドとして保持する必要があります。現在アップロードしているタスクについては、使用しているライブラリでそのタスクを参照する必要があります。あなたがコールバックを得ることができれば、その情報をブロードキャストすることができます。 –

関連する問題