I have a complex app that works, and it has a messaging section. I want to keep it updated with the latest messages that are located on a remote server (mysql)
I need a service to check for new messages on the server. I already did that, but I need to know if it is Ok to implement the service inside the same MAIN app, or is it better to make the service a separate app.
The service uses AlarmManager to fire every minute.
If the better way is to make the service independent then how can I make two different apps use the same SQLite database?
If both things are together (service inside main app) then the SQLite is not an issue since it is located in the same folder.
Please give me a solution and an explication for it.
Thanks
If your service does specific work only for your app, then it should be with in your app.
Since the service runs on the background , it won't affect the activity. You can bind to the service using a service connection and communicate with the service from activity.
Check this link http://developer.android.com/reference/android/app/Service.html on how to bind the service.
Related
I am creating an chat app which is connected to web socket, using a background service. But I should connect it to the activity to show the new messages. Maybe in the main conversations list, or inside the conversation itself.
So, first, I want to check if my service is running, then getting it bound to the opened activity.
And I want the service to tell the activities that there is a new message.
So, is it good to do it using 'bindService'? Because I feel that there's something not right.
You can send messages from the Service to Activities with the BroadcastReceiver class. I would get your information from the following link:
https://developer.android.com/guide/components/broadcasts
I need some help regarding fetch bulk data through background service and store it into android local storage,
I don't want to use pagination, I mean when the app activity is launch that time background service is call and its running until fetching is not completed(50k data row).
Thanks in advance.
:-)
This sounds like something that Android's Services are perfect for. They fire off in the background and are designed to do this exact thing.
Essentially, you just subclass the service and write your download logic in that. Once the download is complete, you can then, depending on how you created your service, notify your app of the result. If the service was bound, you can have the activity that bound to the service register a listener. If the service was started (an Android term for started without an explicit bound recipient) then you can fire of an Intent indicating the completion of the task.
I need to create a test application that will allows the starting/stopping of a persistent background service that runs several threads in the background. (Mainly a WebSocket Server and the Tango location Service). It needs to be persistent so I can start a web browser and connect to the ws socket.
According to what I read, the application should be structured as follows:
Activity -> Service (persistent) -> (Service (Tango) + Thread (WS))
The persistent service needs to be run as a foreground service using startForeground() and as a separate process (set in the manifest) so it doesn't close when the activity is closed.
Now, I got to questions :
1) Is my current understanding correct? Or am I approaching this the wrong way.
2) If I later want to stop the service, I want to start the activity and be able to stop it from there. How does the activity know that the service is running and how does it connect to it? Do I need to implement the binding part? How?
3) Could I achieve 2) using notifications instead and closing it from there?
This is the best I could come up with and so far it seems to work. However, if someone has a better way of implementing this or more "correct" I will change to that answer.
The solution I went for boils down to what I stated above. The only thing is that I had to implement Runnable in new classes to pass the pointers around.
It is very important that no network code is executed in the main thread of the Service, that needs to be in a separate thread.
I need some help or suggestions regarding Background services.
Well I want to achieve this. I have an application with Some Views that application also has a Background Service that always keeps on running.
In my views there is a Button whenever I press that button, that button passes some files to the Background Service and my Background service upload that file onto some server.
I am done with the uploading process. Now I want to know that how can I make a Background Service that always keeps on running and on my tapping of the button it sends a file to the Service.
I am new in Background service implementation.
Please guide Friends with some tutorials, suggestions or guidelines.
Thanks a bunch
You've probably already read some of the Android Service documentation, but I suggest studying it further and looking at the Local Service Sample if you have not done so already:
http://developer.android.com/reference/android/app/Service.html
It sounds like you have already got your Service up and running, and I think the actual problem you are trying to solve now is how to communicate data from your Activity to your Service. When your Activity is bound with a Service that's part of the same application, that service is in the same process and runs on the same main UI thread, so once you get the IBinder object from the Service after binding with it, you can simply directly call the functions in that Service from your Activity. Similarly, you can pass your Service a handler object from your Activity so that the service can send messages or post Runnables to your Activity. Communication with a local Service is therefore quite simple.
So if you take a look at the Local Service Sample in the link above, you will see a section in the code where we get a reference to the Service once binding has completed:
mBoundService = ((LocalService.LocalBinder)service).getService();
After that point, it's possible to directly call methods on that Service that's in the same application. For example, you could have a method called sendFile in your Service. In your Activity, you might do something like:
mBoundService.sendFile( myStuffObject );
There are quite a number of questions on Stack Overflow regarding communicating between an Activity and a Service, and I think you'd find it beneficial to search and read these.
A standard Android service will do just fine in this case.
It will continue running in the background untill its work is finished or until you ask it to stop.
There is a topic on the android dev site explaining services in detail.
you should go for android Service that is used for Background operation . Inside the service your have use TimerTask which will be checking the Queue for every x sec and when any items present in the Queue it will pull the item and upload it to the server.
here is the link for Android Service..
http://developer.android.com/reference/android/app/Service.html
Link fro Queue.http://developer.android.com/reference/java/util/Queue.html
I want to create an application where i need to implement the concept of service. The service concept should be worked out from my activity.
Can anyone help out?
Thanks,
Niki
If your service shall only be manipulated and accessed in your own application, implement simply a local service (See 5. in the Service class documentation).
You can then directly call the service functions from the activity.
If your service is meant to be available publicly, it is a bit more complicated. You'll need an AIDL interface. You can download the Music application source code that includes a service to play audio files in the background and can be controlled by any application. The control is done using Broadcast messages (see BroadcastReceiver).