I have an intentService in my app that I use to play music files in the background.
In this service, I have several methods (play, pause, stop, etc.) that I need to call from my main fragment in response to UI input.
However I cant seem to find any way of sending messages to an intentservice, I can only find ways to sending messages from an intentService to an activity.
If someone knows a way I can achieve this, I would much appreciate it.
Related
I have created an accessibility service for my app where it would detect of the current outgoing call is answered or not but now i want create a callback function to my activity when the call is answered.
I have found some code to make callbacks from service in below links -
Callback from Service
RESTFul API
but i cant figure out a way to do that with my accessibility service, as i am not starting the service, android is, so there is no way to pass an object to the service.
If anyone can help please share.
Your service and your activity are separate processes. They cannot communicate directly. You should look into BroadcastReceiver. Note, though, that the user would have had to start your activity and have it running in the background. If the system decides it is low on memory and kills your activity completely, there is nothing you can do. You will not reliably get this broadcast with your activity in the background. You could try starting a background process from your activity for the sake of finding this event and storing the results somewhere for future consumption. Depends of course on your use case.
I'm quite new to android. I'm building an application that uses the android smack library to communicate with my XMPP server. I am able to send and receive messages perfectly however what I want is to run this ENTIRE sending and receiving messages code to a long term running process in the background and on receiving a certain message I want to launch a screen (something similar to the incoming call screen of android) no matter what the user is doing on his smartphone i.e. when the activity running the messaging service is not open (in a similar way when you get an incoming call from a viber contact and you get faced with the screen even though you obviously dont have the viber application open).
Any ideas on how exactly to do this ?
I have a few ideas in mind after research but dont quite seem to get the correct way and glue all peaces together.
Thanks in advance
you can use intentService to communicate with mainThread
here is a basic examples of intentservice and service
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/
service example
http://www.vogella.com/articles/AndroidServices/article.html
here is an official document
http://developer.android.com/reference/android/app/IntentService.html
I'm struggling to decide which class to use, Activity or Service.
My application is complicated so I will use another example. A timer.
I would like to create an application that start to count the seconds when I open the application and keep counting even when I click the back button.
When I back to the application it will have a gui that show me the time.
So the gui I guess would be the activity and the counting method would be on the service.. but how do I manage that system?how they will be communicating? how can I do that?
You can use BroadcastReceivers and send broadcasts that your activity will be listening to, or you can use Messenger to send messages from the service to the activity
they can communicate with intents, or via shared storage, or content providers, ...
I want to send gps cordinates to a web service whenever the gps position on the phone gets updated.
Should i create a class that extends AsyncTask that sends the data to the webservice from the onLocationChanged event? Or do you guys suggest something else?
I started coding apps yesterday, so please tell me if im on the right track. Thanks
Yes, I think this would be the right approach. Extend AsyncTask and in doInBackground() make the POST/PUT to your web server.
Another approach may be to use a SocketConnection, but this will still have to be operated in a background thread, and may add overhead, to thread Socket disconnection etc.
Just be sure to unregister your BroadcastReceiver in onPause and also handle your AsyncTasks to get cancelled when a new one will be executed, or the Activity stopped... etc, depending on your application needs.
If you would also like to send the location while your app is in background you should consider implementing a Service.
I recommend the IntentService mechanism.
This has a lot of uses and is often overlooked by many Android developers.
I have this basic design in mind:
A main activity will offer a choice of sub activities and also create a Bluetooth service.
The Bluetooth service will reads and buffer live data from a Bluetooth connected device. Enough data , at a fast enough rate (100 to 1000 sps ) so that I don't think it is realistic to use Intents or broadcasting
The sub activities will simply be displaying the same received data but in different way.
Each sub activities will also the user to interact with the data in a different way.
I really prefer that the Bluetooth service is agnostic of the Activity/View onto which the data gets presented.
I'd be willing to 'register' a bunch a 'destination' (which would really be activities) to which 'cooked' data would be sent to. I didn't quite get how to 'register' anything from starting an activity.
How do I pass, for example, a reference to my service to each of those activities? Or it might be the other way around; how do I register each activity to the running service.
Having a C/C++ background, I realize this might not be a good approach in Java.
Thank you.
Ideally this not NOT the best way to approach it. Specifically android is a system that bases its services on passing information via intents from activity to activity, activity to service, service to service.
The way I would approach this would involving having an app on the phone that would be communicating with a service. Specifically the app would receive the data from the service. However; in order to allow the activity to listen to it, you could have the service broadcast a message saying there is new information, and have this activity intercept it. When the service is building this message, you could have information passed via an intent (extra) to the activity. The activity would have a Broadcast Listener inside of it that specifically updates information relative to that service inside of that activity.
And perhaps to keep information from being lost, throw messages into a stack and read it accordingly until its empty (This is assuming you get ton of information)
These links should help
Broadcast Reciever
http://thinkandroid.wordpress.com/2010/02/02/custom-intents-and-broadcasting-with-receivers/
Services
http://developer.android.com/reference/android/app/Service.html
http://marakana.com/forums/android/examples/60.html