I'm working on Android project that related to GPS I still new to android so I don't know a lot about it.
My project is an app where it calculates the distance between user location and a specified other location so if the distance is less than (X) say 1KM the app will send a notification to the user.[[I know how to calculate the distance between two
locations]].
My problem is: how can I do this even if the user didn't open the app (I mean if there's anyway that I can make the app calculates the distance in the background while the app is closed).so I want the GPS feature works in the background.
Thanks in Advance.
You can use a service for background process.
For your question for getting location, you should try these links:
Best way to get user GPS location in background in Android
Get GPS location via a service in Android
My solution for this problem was simply :-
you can use Services for working in the background (when the Activity isn't showing)
check this : -Get GPS location via a service in Android
**if you want this service to run for ever, even if the app is closed/killed you can use broadcast receivers **
To get the app to run in the background, you'll need to learn about a service.
Service is a service that causes applications to run in the background such as gps, notification and more ..
link:
https://developer.android.com/reference/android/app/Service.html
User services. They are simple to use and all you need.
Here is the documentation.
A service is another component of an android application (like activity). You need to define the service in manifest. After that you could start the device using startService (intent). If you need to take some info (communicate with one activity) you can use bindService instead of startService. (Also need to implement onBind).
Another solution is to sendData using a broadcast receiver. Or you can simple set a notification in service and use it.
Related
I'm wondering if you can launch a given Instant App based on the user's location. Is this kind of thing possible? I suppose it would require some kind of server to be always running on the device.
Edit: To clarify a bit, this would be like if App A has a service that tracks the user's location and if they go to a certain spot it would launch Instant App B.
Thanks!
it would need a constant running service and launching the app might be a problem since this particular function could be ransomware like but a notification could be instead that's.
beside that it would a continuous location request at all time.
I have no knowledge in android development nor in ios apps.
Is there a way to get the user's location without my app (android/ios) being open in the background (with the user's acceptance)?
Thanks!
Yes you can have an Android Service that listens for location changes. See LocationManager in service as one example. Depending one what you are doing, the question is how should you start your service.
I am working with the Geofence API Sample application I've got it running and seems to work alright. I did make the modifications to it to use BroadcastReceiver instead of the IntentService.
But I will only get the notifications if I have some other app open that is using GPS such as Maps, or GPS Tester app.
If I don't have one of those apps open and I walk into the geofence zone nothing happens. But as soon as I launch one of them I will get the notification within a few seconds.
I ended up creating a service that forces the GPS to stay active by requesting location updates on a relatively quick interval.
While this is admittedly a poor work around for a "real" application. It worked for my purposes. In which I needed to be able to present a working proof of concept to an audience, which impeded my ability to use a geo points which were further spread out.
Even I too had a struggle with getting Geofence notification as soon I entered Geofence or Exit the one.There were always delays in notifications until I found a really nice Location Library called little-fluffy-location-library which serves my purpose.Location updates will be broadcast to your app periodically.
According the Documentation it says that:
The library works by using Froyo's passive location listener (only possible with Android 2.2 and up, hence why it works best with it), which listens to location updates requested by other apps on your phone. The most accurate location is broadcast to your app approximately every 15 minutes. If a location update hasn't been received from another app for an hour, the library forces a location update of its own.
I think you should give a try with this Library and Let me know if it works for you!!
I have followed this tutorial about how to use the proximity alert and how to register it.
It all works but when I close the app (with the back button, not the home-button) then the proximity alert does not work anymore. Does anyone know how to set it so that it runs in the background?
Thanks in advance!
It seems that you can not get location updates when your application is not running. Receiving location updates in the background is not a good idea since it drains battery power and the user might not be aware that the location is being tracked since the normal behavior for pressing the back button on the application is for it to close all services. If I want my application to continue running in the background, I as a user would press the home button.
Look at the model used by the "My Tracks" application.
Also see the documentation from Android on locations and usage models:
http://developer.android.com/guide/topics/location/obtaining-user-location.html
If you have informed yourself about the reasons not to have the application do this in the background but still want to run this in the background, look into Services
http://developer.android.com/reference/android/app/Service.html
They can run in the background and provide your application with data when asked.
Fundamental part of Android... Services.
http://developer.android.com/guide/topics/fundamentals/services.html
You should get it if you register for it, but keep in mind that proximity alert may not be immediate if your screen is off. If your screen is off, then location is probed for every 4 minutes as stated in the documentation.
I am trying to make an app , where if the user enters a particular region, he will get an alert. And that will work even if the app is in background.
I found Ti.App.iOS.backgroundService to do that for iphone but how to start the background service in android and how to present local notification kind thing when particular latitude and longitude is found.
so basically my questions are :-
background location service in android
android equivalent for localnotification
For background geo on Android I use the below strategy.
1) In my app.js I add an Titanium.App.addEventListener that contains my geo logic
Kitchen Sink Sample https://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/app_events.js
2) I then create a ServiceIntent that fires every 15 minutes
Kitchen Sink Sample https://github.com/appcelerator/titanium_mobile/blob/master/demos/KitchenSink/Resources/examples/android_services.js
Please note this service.js you call needs to be in your android folder, even if you have an android only project.
3) The service checks if the individual has moved more then 1KM, if they have it fires the App Event defined in step 1.
Using this strategy and adjusting my service time and distance calculation I've been able to reduce most of my battery drain issues that I had with the built in location event with distance filter.
Hope this helps.