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.
Related
I am new to Android and I am currently working on a project that requires a lot of Http requests on a web API.
I have been searching for good practices about refactoring AsyncTask subclasses, so I don't have to write a lot of similar code into each Activity class.
Do you have any tips/advices or even an example of how you proceed ?
Thank you.
I think what you are talking about is already answered here In this answer it has a same topic's discussion, Although i also know some blogs that can help you to make it more conceptually clear read them,
Links:
https://trinitytuts.com/reusable-asynctasks-class-in-android/
http://cyriltata.blogspot.in/2013/10/android-re-using-asynctask-class-across.html
I'm writing an application that should both have the ability to place widgets on the home screen, and to show these widgets in the application itself. I figure that the best way to do this is through the use of an AppWidgetHost for the application part.
I've been looking around, but I cannot find any good examples on how to write AppWidgetHosts. Have any of you done this, and are willing to share? Or do you know of any tutorials that I've missed?
Not exactly a tutorial but here is an older forum post.
The code is part of the mylock project, which you also might find useful if you dig.
i need to make a timer service and bind it to an activity... i cant do it...
can someone share an example of such a thing or a tutorial...
ive already gone through
http://developer.android.com/resources/samples/BluetoothChat/src/com/example/android/BluetoothChat/BluetoothChatService.html
and the adnroid developer documention guide
please help
:/
By quickly looking over the BluetoothChatService code you mention, I've seen that the communication between the activity and the service goes through the Android Handlers.
Try to take a look on the developer documentation on how to use them. Here are also some quite good resources:
http://developer.android.com/reference/android/os/Handler.html
http://www.tutorialforandroid.com/2009/01/using-handler-in-android.html
It would be nice if StackOverflow had a section where we could post tutorials like mine so that I can answer literally hundreds of questions that have been asked here with a single blow. See... every day I read questions about how to pass complex objects between activities, how to save state so that your app can resume after orientation change, how to update listviews when the data changes, etc, etc.
Here is the second part of a tutorial series I posted on my blog... I hope that you actually read it... because I haven't seen any examples like it anywhere... and it has changed how I think about developing for Android across the board. The question is... is there a downside or negative affect of developing like this?
Beyond Smart Lists – How Observable Singletons change the game.
Please read through both of these tutorials carefully... I will answer any questions about it here that I can... I really want to know what you think about this and if it might solve issues for you.
NOTE TO MODERATORS: there are no advertisements of any kind on my blog.. so don't just close this because you think I am spamming somehow... I am not going to duplicate my post here. And... really I want to know if there is a flaw in this approach.
Have you read about Android's Application class?
Application's javadoc
sharing-domain-objects-between-activities.
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.