android - How to notify my main activity, that thread has finihed? -



android - How to notify my main activity, that thread has finihed? -

i need create simple thing: from app have create photo, send server (in background) , show notification after sending. works, have wait end of sending file - activity photographic camera closing after that. don't want wait, want main activity right after taking image (but upload still going in thread, , send notification when finishes).

the problem: don't know how allow know main activity, thread has finished uploading of photo. maybe passing context or handler photographic camera activity help, can't putextra().

any suggestions?

some fragments of code:

in mainactivity.java:

intent intent = new intent(this, camera.class); startactivityforresult(intent, constants.requestcode_camera);

in camera.java:

@override protected void oncreate(bundle savedinstancestate) { //(...) intent intent = new intent(mediastore.action_image_capture); intent.putextra(mediastore.extra_output, photouri); startactivityforresult(intent,take_photo); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { //(...) new sendfiletoserver().execute(); // finish(); // finish camera.java here, , mainactivity, while sendfiletoserver uploads file , send notification later } protected class sendfiletoserver extends asynctask<void, void, string>{ @override protected string doinbackground(void... params){ //(...) // here sending of file server, works } @override protected void onpostexecute(string result) { //(...) // code below doesn't work, because didn't pass "context", don't know how, or it's impossible mainactivity mainactivity = (mainactivity) context; mainactivity.sendfilename(filename); } }

what can send broadcast on end of asynctask , register receiver in activity want (e.g. show notification).

to need to:

1) pass application context asynctask (in activity):

@override protected void onactivityresult(int requestcode, int resultcode, intent data) { //(...) new sendfiletoserver(getapplicationcontext()).execute(); // pass application context asynctask finish(); }

2) send broadcast onpostexecute():

protected class sendfiletoserver extends asynctask<void, void, string>{ private context context; public sendfiletoserver(context context) { this.context = context; } @override protected string doinbackground(void... params){ //(...) // here sending of file server, works } @override protected void onpostexecute(string result) { intent intent = new intent("com.example.upload_finished"); context.sendbroadcast(intent); } }

3) register broadcastreceiver in mainactivity (don't forget unregister receiver in onpause()):

mainactivity.class

private broadcastreceiver broadcastreceiver; @override protected void onresume() { super.onresume(); intentfilter intentfilter = new intentfilter( "com.example.upload_finished"); broadcastreceiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { // show notification } }; registerreceiver(broadcastreceiver, intentfilter); } @override protected void onpause() { super.onpause(); unregisterreceiver(broadcastreceiver); }

android multithreading android-intent android-activity android-asynctask

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -