【发布时间】:2017-02-10 12:31:48
【问题描述】:
嗯,我正在考虑这个问题,我需要社区的帮助。
我想将图片(5 张图片)上传到当前在应用程序的异步任务中完成的服务器。因此,当用户关闭应用程序时可以停止上传,所以我想使用 IntentService 执行此操作。
所以我创建了我的 Intent 服务来执行图像上传。
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.假设从app出来回到activity,如何知道当前上传的图片是什么?
2.如何在回调中访问onHandleIntent() 中的意图数据?,因为在意图数据中我有关于imageType 的信息,所以当请求成功时,我想发送带有imageUrl 和imageType 的广播。
【问题讨论】:
标签: android service android-intentservice