gpt4 book ai didi

android - 从 Activity 中更新 IntentService 的数据

转载 作者:行者123 更新时间:2023-11-30 01:33:07 25 4
gpt4 key购买 nike

我有一个 IntentService 可以将图像上传到服务器(来源如下)。在上传过程中,用户可以选择更多图像进行上传。现在它产生了另一个服务。如果服务未运行,我如何将数据添加到同一正在运行的服务或生成新服务

public class UploadService extends IntentService {

public static final String TAG = UploadService.class.getSimpleName();

public static final String ACTION_UPLOAD = UploadService.class.getName() + ".UPLOAD";
public static final String EXTRA_RESOURCE_ID = "resource_id";
public static final String EXTRA_RESOURCE_NAME = "resource_name";
public static final String EXTRA_IMAGES = "images";

private static final int CONCURRRENT_UPLOAD_COUNT = 3;
private static final int UPLOAD_STATUS_PENDING = 0;
private static final int UPLOAD_STATUS_UPLOADING = 1;
private static final int UPLOAD_STATUS_UPLOADED = 2;
private static final int UPLOAD_STATUS_FAILED = 3;
private static final int UPLOAD_STATUS_TERMINATED = 4;

private ArrayList<DiskImage> mImages = new ArrayList<>();
private ArrayMap<DiskImage, Integer> mStatusMap = new ArrayMap<>();
private OkHttpClient mOkHttpClient = new OkHttpClient();

public UploadService() {
super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {
if (intent.getAction().equals(ACTION_UPLOAD)) {
String resId = intent.getStringExtra(EXTRA_RESOURCE_ID);
String resName = intent.getStringExtra(EXTRA_RESOURCE_NAME);
ArrayList<DiskImage> images = intent.getParcelableArrayListExtra(EXTRA_IMAGES);
for (DiskImage image : images) {
image.mUploadId = resId;
image.mUploadResName = resName;
}
mImages.addAll(images);
upload(); // start the upload
updateNotification();
}
}

private int getUploadedImageCount() {
int count = 0;
for (int i = 0; i < mImages.size(); i++)
if (getStatus(mImages.get(i)) == UPLOAD_STATUS_UPLOADED)
count++;
return count;
}

private void updateNotification() {
UploadNotification.notify(UploadService.this, mImages.size(), getUploadedImageCount());
}

private void upload() {
final DiskImage image = getImageToUpload();
if (image != null) {
// show notification
MediaType mediaType = MediaType.parse(image.mimeType);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("resource_id", image.mUploadId)
.addFormDataPart("resource_name", image.mUploadResName)
.addFormDataPart("image", getName(image.path),
RequestBody.create(mediaType, new File(image.path)))
.build();
Request request = new Request.Builder()
.url(getUploadUrl())
.post(requestBody)
.build();
mStatusMap.put(image, UPLOAD_STATUS_UPLOADING);
// Log.i(TAG, "Uploading image " + getName(image.path));
mOkHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
mStatusMap.put(image, UPLOAD_STATUS_FAILED);
// add failed image in end
mImages.remove(image);
mImages.add(image);
// Log.i(TAG, "Upload failed " + getName(image.path));
upload();
e.printStackTrace();
}

@Override
public void onResponse(Call call, Response response) throws IOException {
// Log.i(TAG, "Uploaded image " + getName(image.path) + " Response: " + response.body().string());
mStatusMap.put(image, UPLOAD_STATUS_UPLOADED);
upload(); // upload next image
updateNotification();
}
});
}
}

private String getName(String path) {
return path.substring(path.lastIndexOf(File.separator) + 1, path.length());
}

private DiskImage getImageToUpload() {
for (int i = 0; i < mImages.size(); i++)
if (getStatus(mImages.get(i)) == UPLOAD_STATUS_PENDING)
return mImages.get(i);
return null;
}

private String getUploadUrl() {
return String.format(Constants.ALBUM_IMAGE_UPLOAD_URL, Preferences.getToken(this));
}

private int getStatus(DiskImage image) {
if (mStatusMap.get(image) == null)
return UPLOAD_STATUS_PENDING;
return mStatusMap.get(image);
}

}

最佳答案

你不能只是扩展服务(不是IntentService),将你的 Activity 绑定(bind)到服务然后调用这个服务的方法,它会上传图片。

这样,您的服务将一直运行,直到您在服务中调用 stopSelf() 或在有界 Activity 中调用 stopService()。因此,您可以使用相同的服务进行多次上传。

如果这个答案对您有帮助,请告诉我...

关于android - 从 Activity 中更新 IntentService 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35504562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com