Best Way to make Service using location data - java

I'm creating an Android application, that makes some logging tags, while user is moving.
I decided to create three Service.
The First service is managing log in internet, geting commands to get location and to write location.
The Second service is managing location data: transforming raw location data from google api ( make some approximation and calculation ), putting it into nolockingqueque and giving last sample from it.
The Third service is something like this: https://gist.github.com/blackcj/20efe2ac885c7297a676 . Service, which is getting raw location data and raising broadcast, that location data had been updated.
Broadcast reciever Binds to the Second service, gives him location data.
I'm filling that this way is too hard and it is not the best.
In fact I need three things in application:
some container with computedLocationInformation
Thread or service which will get Raw Location Data from Google Services
Service which will use computedLocationInformation.
Can you give me better architecture for this issue, please?
Thank you.

it can be done within a single service. You can create three different thread in the single service and all three thread will do their own job but its all depends on your logic how you will manage all these operations

Related

Use of global variable in never ending service

I created a never ending service but I'm not sure that it's OK to do what I did with this service. I declared global variable like static String list_contact
and fill the list from a db in onCreate method. I wanted to avoid to retrieve data from database each time because I need to compare to the list fast. But maybe it's a problem to store data in global variable of Service. Plus I need another list with thousands of data. If it's no good, can I find a compromise between memory and speed for retrieving data ? Thanks.
Its not a great idea put the static variable in Service and assume that service would run forever.
The biggest drawback is, service won't run in background forever, starting from Android O. Latest version applies more restrictions on the background processing and kills your service few minutes after app is put in background.
You can implement Object pool pattern which will store the frequently accessed data in an Object. This doesn't require to have a service running and could serve your purpose. You need to ensure that you are not maintaining sensitive data in clear text in these Objects.
Alternatively, you can also read this post which describes caching for Android.

Communication between objects, broadcastreceivers and services

I have made this app that offers it's functionality almost completely in a notification. The configuration is done in the app itself. This is the structure I have now:
Service 1: retrieve data and changes to that data from FireBase and save it in an object. Everything the app needs to show in the app itself is in this object. It gets distributed to other activities via the observable pattern. So these activities implement Observer.
Service 2: this service keeps track of external changes that can directly affect the object in service 1. Therefore, this service is bound to service 1.
Activities: some activities to display the data. All these activities implement Observer and observe the object that's stored in Service 1.
So far so good. The above gives me no problems whatsoever. I can communicate with both services from every activity. Every activity gets an update if the object they are interested in changes and figures out what to do with the updated data.
Now I have this notification, all of it's actions are broadcasted intents, for which I have a BroadcastReceiver that handles these intents. The broadcastreceiver may need data from Service 1 to execute it's action, and all actions are located in simple classes (no activity or service), but I can't bind a broadcastreceiver or objects to a service.
As I see it I have 2 possible solutions here:
Create another service in which all possible "notification" actions are located. This service can be bound to Service 1 and thus can retrieve all the data it needs.
Make the observable object in Service 1 static, so even the objects in which the "notification" actions are located can access it.
Possible problems for solution 1: maybe 3 services is a little over-kill? But putting all functionality in the same service would create a big, unclear service.
Possible problems for solution 2: I've read that static variables are not best practice, because static variables represent global state and are hard to reason about. Source
I hope someone can tell me if I'm right on the above assumptions, and if there's a solution I have not mentioned here.

Android - Single data request class that handle a lot of activity

Im new to android and just learning how to pass data from server using json and volley.
what i want to ask is:
if i have a lot of activities on my apps, and each of every activity need to take data from the server, and I need to use json in every single class, and put the requeststring in it, which is very redundant.
Is there a way to put all the functions in just 1 class (maybe static), so I will have 1 class that only handling the data request from the client to the server. I found it really confusing, because at the same time, I need to modify the UI when the apps received the data (eg. move to another activity, or just simple change the textview).
So, basically the class will check whether the data exist on the database, if not, it will request the data from the server, and return it to the activity
it is possible to do this? Is there any tutorial that I can refer to?
Thanks a lot! cheers!

Background Process to scan the location of the user at regular intervals and update the local database even when the app is not open

I am creating an app that checks for user locations every half an hour and updates the location of the user in the local database and then runs CRUD queries based on the user's location even when the app is not running. How do i do it ?
I have referred to this http://techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html article and i am still confused about which is the correct approach for my result ?
There are 4 options according to the article for what i intend to achieve according to me
1) Service : But since i feel it would be a long operation with the local database, i feel i should ignore this one.
2) IntentService : This cannot perform multiple tasks, so i feel this one also should be avoided for me as i have to get the location of the user and scan the database , update the database (3 tasks)
3)Thread : I am not sure how to call this when the app is not open
4) AsyncTask : I am not sure how to call this when the app is not open.
Basically i looking for something like a CRON JOB that runs on a local database while working on the location data.
It would be great if you could link me up to some tutorials and answer with a simple example to make me understand the difference of all 4 methods.
// editted on 16 March :
I have read something about a JobScheduler which is introduced in the API 21, but not sure if it also supports till Gingerbread and is it the right approach for my question
Thanx
When recording the users position use a service with a notification. Just for the sake of creating a morally responsible app that informs the user the app is tracking them. The service by definition runs in the background.
A fused location provider with setinterval(long) 30 minutes gets the interval. Set fastestInterval() to a minute to receive GPS data when other apps are using the GPS.
Have you considered using a SyncAdapter. Its best to schedule jobs at fixed interval and also optimized for battery usage. Also, once started, it can run independently of the app. As per your requirements, I believe this is best suited for your need. You can read about this here. This also removes the corner case of starting the service (generally used) when your device is restarted. Your app will still continue running the scheduled job even if the device gets restarted.
In the SyncAdapter you have to use a ContentProvider so wrap your DB inside a ContentProvider. Also, preferably use a CursorLoader to run longrunning tasks on DB. You should read about CursorLoader. This is a great way to access your resources. Also, you can define an Observer Design Pattern which Observes for changes in a DB and will perform a task when changes are made in DB. This can also be used inside your application itself and also inside SyncAdapter. Cursor Loader is best preferred for background work on DB. You can perform all CRUD Operations using a CursorLoader and ContentProvider.
This cannot perform multiple tasks
Yes, it can. It has only one thread, and so it can only do one simultaneous task.
i have to get the location of the user and scan the database , update the database (3 tasks)
I have no idea why you think that is three tasks. You cannot do them simultaneously.
Your bigger problem with IntentService is that getting location data is asynchronous, and IntentService is not well-suited for calling APIs that themselves are asynchronous.
But since i feel it would be a long operation with the local database, i feel i should ignore this one.
The point behind any service is for "a long operation".
Basically i looking for something like a CRON JOB that runs on a local database while working on the location data
Use AlarmManager to trigger a WakefulBroadcastReceiver, which then triggers a Service. The Service, in onStartCommand(), forks a background thread to (asynchronously) retrieve the location and update the database. The Service can then call completeWakefulIntent() on WakefulBroadcastReceiver, plus stopSelf() with the startId received in onStartCommand() for this work, plus allow the thread to terminate. If no other commands were received in the interim, the service will shut down.
I think you are looking for something similar to WakefulIntentService. This handles all your cases completely.
You can do your location and db related work inside doWakefulWork() of said implementation.
I've done what you are looking for, both with GPS and non-GPS.
The project I took as staring point for the non-GPS solution already does all you need, and is battery-friendly (credits should go to Kenton Price):
https://code.google.com/p/little-fluffy-location-library/
Take a look at it, it works like a charm. Just run it in any device. If you need any help customizing just let me know.
Just edit the "onReceive" method in the "TestBroadcastReceiver" to update your DB.
If you need the GPS solution let me know too, but I dropped it for being a battery killer!
Hope it helps.
1. I think for this requirement, Thread and inside it AsyncTask -- this structure will be useful.
In link provided by you, it is mentioned very nicely here
2. For location related blog, you can check useful materials here :
(1) Difference between Google Map Distance and Directions API
(2) Check this answer also
Hope this will help you

Pausing and notifying particular threads in a Java Webservice

I'm writing a Java webservice with CXF. I have the following problem: A client calls a method from the webservice. The webservice has to do two things in parallel and starts two threads. One of the threads needs some additional information from the client. It is not possible to add this information when calling the webservice method, because it is dependent from the calculation done in the webservice. I cannot redesign the webservice becuase it is part of a course assignement and the assignements states that I have to do it this way. I want to pause the thread and notify it when the client delivers the additional information. Unfortunately it is not possible in Java to notify a particular thread. I can't find any other way to solve my problem.
Has anybody a suggestion?
I've edited my answer after thinking about this some more.
You have a fairly complex architecture and if your client requires information from the server in order to complete the request then I think you need to publish one or more 'helper' methods.
For example, you could publish (without all the Web Service annotation):
MyData validateMyData(MyData data);
boolean processMyData(MyData data);
The client would then call validateMyData() as many times as it liked, until it knew it had complete information. The server can modify (through calculation, database look-up, or whatever) the variables in MyData in order to help complete the information and pass it back to the client (for updating the UI, if there is one).
Once the information is complete the client can then call processMyData() to process the complete request.
This has the advantage that the server methods can be implemented without the need for background threads as they should be able to do their thing using the request-thread supplied by the server environment.
The only caveat to this is if MyData can get very large and you don't want to keep passing it back and forth between client and server. In that case you would need to come up with a smaller class that just contains the changes the server wants to make to MyData and exclude data that doesn't need correcting.
IMO it's pretty odd for a web service request to effectively be incomplete. Why can't the request pass all the information in one go? I would try to redesign your service like that, and make it fail if you don't pass in all the information required to process the request.
EDIT: Okay, if you really have to do this, I wouldn't actually start a new thread when you receive the first request. I would store the information from the first request (whether in a database or just in memory if this is just a dummy one) and then when the second request comes in, launch the thread.

Categories