我正在使用timertask在s3上上传图像,但是由于后台进程的限制,现在在Oreo +设备上面临问题。
现在,我已将代码迁移到JobIntentService中。
我写了从JobIntentService继承的S3UploadService,该服务的主要目的是在s3存储桶上上传图像。
我正在使用TransferUtility进行上传。在服务完成上传之前调用服务销毁,原因是transferutility正在执行异步任务。
build.gradle
依赖性
api 'com.amazonaws:aws-android-sdk-s3:2.2.+'
public class S3UploadService extends JobIntentService {
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, S3UploadService.class, S3_UPLOAD_SERVICE_JOBID, work);
}
@Override
public void onCreate() {
super.onCreate();
Log.d(S3_UPLOADSERVICE_LOG_TAG, "S3UploadService - onCreate");
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
Log.d(S3_UPLOADSERVICE_LOG_TAG, "S3UploadService - onHandleWork start");
RequestProcessor requestProcessor = new RequestProcessor(this);
requestProcessor.sendS3ContentRequest();
Log.d(S3_UPLOADSERVICE_LOG_TAG, "S3UploadService - onHandleWork finish");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(S3_UPLOADSERVICE_LOG_TAG, "S3UploadService - onDestroy");
}
}
public class RequestProcessor {
public void sendS3ContentRequest() {
TransferObserver transferObserver = transferUtility.upload(Utility.getValue(context, "bucket") "/" additionalBucket, fileName, file, om,CannedAccessControlList.PublicRead);
s3ObserverMap.put(transferObserver.getId(), fileInfo.getFileName());
transferObserver.setTransferListener(new TransferListener(){
@Override
public void onStateChanged(int id, TransferState state) {
// do something
switch (state) {
case COMPLETED:
Intent intent = new Intent("s3-upload");
intent.putExtra("filename",filename);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
break;
case CANCELED:
case FAILED:
break;
}
}
}
Log.i("info", "onStatechanged " id " state " state.name());
}
}
}
是否有任何方法/标志可以从服务onHandleWork调用异步作业/任务?并在工作完成后销毁。