この問題を考えているので、コミュニティの助けが必要です。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
についての情報を持っているのでので、私はimageUrl
とimageType
でブロードキャストを送信したいです。
現在実行中のタスクを特定するにはどうすればよいですか?コールバック内のインテントデータにどのようにアクセスできますか? – Manikanta
インテントデータはフィールドとして保持する必要があります。現在アップロードしているタスクについては、使用しているライブラリでそのタスクを参照する必要があります。あなたがコールバックを得ることができれば、その情報をブロードキャストすることができます。 –