【问题标题】:Android perform ImageUpload in background and access intent data in callbackAndroid 在后台执行 ImageUpload 并在回调中访问 Intent 数据
【发布时间】: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 的信息,所以当请求成功时,我想发送带有imageUrlimageType 的广播。

【问题讨论】:

    标签: android service android-intentservice


    【解决方案1】:

    这可能不是理想的答案,但我通常在服务和活动上都有广播接收器。这样他们就可以互相交流了。例如,当活动再次开始时,我向服务发送广播通知它。然后该服务将发送一个包含其信息的广播。

    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;
            }
        };
    
    }
    

    【讨论】:

    • 如何识别当前正在执行的任务?以及如何在回调中访问意图数据?
    • 您应该将您的意图数据保留为一个字段。至于当前的上传任务,您必须在您正在使用的库中查找。如果您可以从中获得回调,您将能够广播该信息。
    猜你喜欢
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2013-02-02
    相关资源
    最近更新 更多