有沒有辦法阻止當前線程並等待圖片上傳到S3?在Android上將圖片上傳到Amazon S3時阻止線程
我的上傳到S3的代碼已經在doInBackground()方法的Android服務中運行(其他網絡調用必須按照特定的順序),所以它不在主UI線程中。我只想知道上傳何時完成,因此我可以關閉通知並向用戶顯示上傳/完成的所有內容。
我使用SDK v2的,就像這樣:
TransferUtility transferUtility = new TransferUtility(s3, ctx);
TransferObserver obs = transferUtility.upload(
S3BucketNamePhotoTEST,
imageId + ".jpg",
file, new ObjectMetadata());
obs.setTransferListener(new UploadListener(dbHandler, ctx, imageId));
凡UploadListener是一個類實現TransferListener,其中onStateChanged()方法中,我們可以發現,當上傳完成後:
public void onStateChanged(int id, TransferState state) {
if (state == TransferState.COMPLETED) {
//do something here!
}
}
這不是我需要的,因爲當onStateChange()回調觸發時,我的服務調用將會很長時間。
但在SDK V1看(這是現在大多不建議使用),有阻止它的方式,與:
TransferManager tx = new TransferManager(
credentialProviderChain.getCredentials());
Upload myUpload = tx.upload(myBucket, myFile.getName(), myFile);
...
// Or you can block the current thread and wait for your transfer to
// to complete. If the transfer fails, this method will throw an
// AmazonClientException or AmazonServiceException detailing the reason.
myUpload.waitForCompletion();
這waitForCompletion()方法正是我所需要的,但不能得到它與更新的API。
任何解決方案?
對不起安德魯,但是,這並不回答我的問題。我需要阻止來自我稱之爲transferUtility的上傳功能的線程。這段代碼並沒有解決這個問題。 – Beemo
但上傳的代碼已經離開UI線程(在服務線程上),所以在阻止它的時候沒有問題。我保持在該服務中同步的邏輯(不僅僅是照片,所有類型的其他應用程序),而且如果服務完成並且用戶認爲所有內容都已上傳,但是上傳照片在其他線程上等待回調,這很愚蠢亞馬遜火。 – Beemo
我對你在OP中談論的內容感到困惑。我已經更新了我的答案。 –