multithreading - Block thread while uploading picture to Amazon S3 on Android -


is there way block current thread , wait while picture uploaded s3?

my code uploading s3 running in android service in doinbackground() method (with other network calls have in specific order), it's off main ui thread. want know when uploading done, can dismiss notification , show user uploaded/done.

i'm using sdk v2, this:

transferutility transferutility = new transferutility(s3, ctx); transferobserver obs = transferutility.upload(             s3bucketnamephototest,             imageid + ".jpg",             file, new objectmetadata()); obs.settransferlistener(new uploadlistener(dbhandler, ctx, imageid)); 

where uploadlistener class implementing transferlistener, in onstatechanged() method can detect when upload finished:

public void onstatechanged(int id, transferstate state) {         if (state == transferstate.completed) {         //do here!         } } 

that's not need, because service call long done when onstatechange() callback fires.

but looking @ sdk v1 (that's deprecated), there way block it, with:

transfermanager tx = new transfermanager(            credentialproviderchain.getcredentials()); upload myupload = tx.upload(mybucket, myfile.getname(), myfile); ... // or can block current thread , wait transfer // complete. if transfer fails, method throw // amazonclientexception or amazonserviceexception detailing reason. myupload.waitforcompletion(); 

this waitforcompletion() method need, can't newer api.

any solutions?

edit

if service ending before able utilize callbacks, need keep service open, doesn't happen. if you're using intentservice, these designed kind of "fire-and-forget" feature. should @ implementing normal service can start task , end when need via stopself().

check out http://developer.android.com/guide/components/services.html#extendingservice

this give more control of when service stops. can stop service when upload complete.

or, if want "hack" it, add boolean member variable , block execution using loop, poll boolean until true. can set true in uploadlistener method, , should cause service stay open.

boolean muploadcomplete = false;  // start upload. 

then after obs.settransferlistener(new uploadlistener(dbhandler, ctx, imageid));

do {     thread.sleep(1000); } while (!muploadcomplete); 

in:

public void onstatechanged(int id, transferstate state) {     if (state == transferstate.completed) {         muploadcomplete = true;     } } 

/////////////////////////////// ** previous revision below **

this looks solve problem.

uploadresult waitforuploadresult()                         throws amazonclientexception,                                     amazonserviceexception,                                     interruptedexception 

waits upload complete , returns result of upload. blocking call. prepared handle errors when calling method. errors occurred during asynchronous transfer re-thrown through method.

so call:

uploadresult myresult = myupload.waitforuploadresult(); 

that should block thread until upload finished.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -