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()
内回调?因为意图数据我有,所以当请求成功,我想与imageUrl
和imageType
发送广播有关imageType
信息。
如何识别当前正在执行的任务?还有如何访问回调中的意图数据? – Manikanta
您应该将您的意图数据保存为一个字段。至于目前的上传任务,你将不得不在你正在使用的库上查看。如果您可以从中获得回拨,您将能够广播该信息。 –