How to let a thread communicate with another activity thread - android

i'm beginning to learn android, i meet a problem in my project,
in my application,i create a background thread which get data from remote server by UDP,
in this thread ,i will parse the data and distribute the message to different activity to process, so i don't know weather is there a mechanism to handle this problem.
thanks for your answer

You should take a look at this article about painless threading in android, and pick a solution that best fits for you.
If you retrieve data from the server repeatedly, maybe you should go with a Service - Handler solution, but AsyncTask seams to be the easiest for me.

I would recommend you to go through the dev guide as suggested by #UBM. Also, if you are looking for different threading constructs and communication, I would strongly recommend you to look at a series of articles on Android threading constructs starting with this article.

Related

Android design patterns for REST calls with AsyncTask

I'm developing an app in which I pretend to invoke a REST service for typical CRUD operations. Since I want to separate the requests processing from the UI thread, I'm planning to use an AsyncTask to do the separate work. However, my question here is: how should I desing my AsyncTask(s) model? Should I use one AsyncTask for all CRUD operations (is this even feasible?), or use i.e. 4 AsyncTasks (create, delete, update, retrieve)?
Thanks in advance
I would go with a ContentProvider instead of using AsyncTask.
According to this thread:
https://groups.google.com/forum/?fromgroups#!topic/android-developers/8M0RTFfO7-M%5B1-25%5D
on Android 4 AsyncTasks will be sequential.
So, for that reason alone your solution may be less than optimal.
But, ContentProvider just makes more sense for what you are trying to do, as what happens behind the CRUD calls the user doesn't care. You may want to run this on a separate thread though, as being on the UI thread for too long is bad.
It is really up to you, and perhaps, the REST service which you are consuming.
Some things to consider:
Is the service general enough that it would be easy to do everything in a with a single AsyncTask?
Will your code be easier to read and understand if you do things with a single AsyncTask? I would tend to think that it would be easier to read if you did one AsyncTask for each operation. (ie, CreateTask, UpdateTask...)
Will I get the reusability that I am after with my choice (whatever it is)?
Personally, I would create 4 distinct AsyncTask's, and any reusable code I might put in a base class, but again, it is really up to you for what is going to work best for you.
Personally, I use the Loader framework, available with the compatibility library. I have a subclass of AsyncTaskLoader for each of the CRUD operations, and it works really well!
I guess if you prefer to avoid having lots of classes you could use the same task (i.e. the create and update could technically fit in the same task) - when you subclass your AsyncTaskLoader create a setParameters(...) method which you can call when you create the loader in onCreateLoader().
It might help to read the above after you've reviewed the Loader documentation.

Android Client with Asynchronous Way

I am trying to make a client with asynchronous. I am not good in Java. Then its a bit difficult for me. I want to recieve and send data as much as performance.. And I think Asynchronous is the best option for it? I wanted to use cometd but its more complicated.. anyway What can I do for it? or Which way I should follow?
Thank you
Take a look into AsyncTask. There are various ways to do work on separate threads. See here.

Recommended resources for understanding concepts in Android

After a relatively easy coast to simple app coding, I would like to understand better the intricate relationships between various conceptual components in Android.
More specifically, I would like to understand what is Runnable, Looper and Handler.
As you may noticed, the above 3 terms are links to formal documentation in http://developer.android.com so my question may seem strange, so let me explain: That documentation may be perfect for someone who already understands how things work in Android, but I need something that sequentially walks through fundamentals, building on top of previous concepts.
To summarize, I need some sort of tutorial on core inner building blocks of Android. Can you recommend one?
The detailed article Painless Threading is probably your best resource for threading on Android.
The moral of the story is that AsyncTask makes multithreading easier for you.
Runnable is a core Java interface - it represents a code part that can be run (usually by a specific thread).
Handler is an Android class that is responsible for posting a Runnable\Message so that a particular thread will run or process them (in a specific order).
Looper is the structure that holds the Runnable\Message queue that a HandlerThread will read from.

Updating android from service

I am searching for a solution to update the UI from a service.
The best solution I found is this one:
subclass Application and let in-app
communication go through this class
As suggested here More efficient way of updating UI from Service than intents?.
Unfortunately I am new to Android and Java programming, so I do not really understand how this works.
It would help me very much, if someone could create a working example project from the given code snippets in the solution. An app that starts a service, increases a counter and displays the counter in textview would be a good and simple example. I think I will understand this much better than a therotical solution.
Here is an answer to a similar question where there is a full example. If you are just doing some basic background task, an AsyncTask may be a better solution though.

Android Services

i have a question. I'm developing an Android Application. Actually, i have a thread in background that makes request to an external API in order to get data when the users clicks in different parts of the app.
My doubt is if this "thread" would be better if i implemented it as a service instead of a Runnable class.
Thanks
The point of a service is that it can run while your app is not resident. If you only want the service to run while your app is open then a thread is the probably the best way to do it.
As i learned some days ago, using AsyncTasks is the preferred, painless way in android to do background tasks. Have a look here to get a good tutorial.
http://android-developers.blogspot.com/2009/05/painless-threading.html
bye
If your information can be used by any other application you could use a service or as they are called in Android, a Content Provider. This way you make the information available to all the applications on the phone. This is a great way of encouraging other developers to build their applications with the info that you've provided. This is just something that you should consider, if it's something strictly related to you're application you can go on with the thread just as CaseyB stated.

Categories

Resources