开发者

Android, How to send a message back to main thread

开发者 https://www.devze.com 2023-03-08 16:05 出处:网络
I actually have three questions.. Let\'s say I have a function called CalcualteAge() and this function is going to be called by several users (Lets think about this as a simple server application pro

I actually have three questions..

Let's say I have a function called CalcualteAge() and this function is going to be called by several users (Lets think about this as a simple server application providing this function) then how would I hand开发者_JS百科le concurrent function calls between users? (Ex. What happen if user A and user B call the function at the same time

Another question is that the function (CalcualteAge()) is running on a separate thread and it has a return value. Then how would I pass the return value back to the main thread so that main thread can return the value back to the person who called the function.

Last one is that which way is better to implement this? Should I create a new thread and run the function on the thread and terminate the new thread once it finish the calculation? or Should I keep the thread running on background as long as the the application is running?

Thanks in advance.


You can get values through broadcast receiver......as follows, First create your own IntentFilter as,

Intent intentFilter=new IntentFilter();
intentFilter.addAction("YOUR_INTENT_FILTER");

Then create inner class BroadcastReceiver as,

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    /** Receives the broadcast that has been fired */
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction()=="YOUR_INTENT_FILTER"){
           //HERE YOU WILL GET VALUES FROM BROADCAST THROUGH INTENT EDIT YOUR TEXTVIEW///////////
           String receivedValue=intent.getStringExtra("KEY");
        }
    }
};

Now Register your Broadcast receiver in onResume() as,

registerReceiver(broadcastReceiver, intentFilter);

And finally Unregister BroadcastReceiver in onDestroy() as,

unregisterReceiver(broadcastReceiver);

Now the most important part...You need to fire the broadcast from wherever you need to send values..... so do as,

Intent i=new Intent();
i.setAction("YOUR_INTENT_FILTER");
i.putExtra("KEY", "YOUR_VALUE");
sendBroadcast(i);

....cheers :)


If you are doing work on a background thread, you can deliver results on the main thread using a Handler. But the model of "return value from a function" is wrong. You need an asynchronous architecture. The most common one is a call-back function resultAvailable that can be called at any time after the background thread has done its job. The AsyncTask class can help a lot with this.

Whether to create a separate thread each time or keep a background thread running depends on how frequently you need this calculation done. You might want to use a producer/consumer model for this. There are many examples on the web of how to implement this in Java.

An alternative architecture, perhaps more appropriate for what you describe, is to set up your calculation code as a Service. Then you can "call" it by constructing an intent with the request and receiving the result. See the guide section on Services for details.

0

精彩评论

暂无评论...
验证码 换一张
取 消