How to send location of the device on server when needed
I would definitely suggest using Google Cloud Messaging
here. This has the benefit that once a device runs your app, you can make it register against GCM
and you can send them some message that would make them register their coordinates into your database.
This approach would need that you have to implement some server (for instance, a web server with PHP) and make the users communicate with it, so the flow would be:
Your server sends a broadcast message to all registered devices telling them they have to register against the
GCM
service.The devices get the message, and you implement the
BroadcastReceiver
to get both latitude and longitude and send it to your remote server, sayhttp://www.mysuperserver.com/getlonglat.php
, via aPOST
request.Your server takes both parameters and you save them or do whatever you need.
If you want to follow this approach, this are the steps you have to follow:
Go to https://console.developers.google.com. With your Google account, register a new project. This will provide you an API key and a Sender ID.
You'll need to make the device register against your project in
GCM
. You can achieve this by importingGoogle Play Services
within your project, and once done, do something alike to this:GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
final String regid = gcm.register("YOUR_SENDER_ID");At this time, the
GCM
server already knows that this user has registered whithin your project and has given him aregid
.The next step is informing your own remote server that the user has done so, and save somewhere the
regid
that it has been given, so you can later send them messages. So in the client side, make aHTTP POST
request against your server sending thatregid
and process it with somethat like this:$regid = $_POST['regid'];
if (($regId) && ($ipAddr))
sql_query("INSERT INTO gcm_subscribers(regid) VALUES($regid')");Once done, you know who have already registered against your database. You can
SELECT
all the users and send them a signal to send you back their coordinates. In the following example, the parameters would be, firstly, an array of registration IDs and secondly the message to send (for instance,$messageData = "sendMeYourCoords!"
).function sendNotification($registrationIdsArray, $messageData) {
$apiKey = "YOUR_API_KEY";
$headers = array("Content-Type:" . "application/json", "Authorization:" . "key=" . $apiKey);
$data = array(
'data' => $messageData,
'registration_ids' => $registrationIdsArray
);
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_URL, "https://android.googleapis.com/gcm/send" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($data) );
$response = curl_exec($ch);
curl_close($ch);
return $response;
}Once in your client, just process that
GCM
message and make anotherPOST
to anotherURL
on your remote server and pass it thelongitude
andlatitude
. To process the messages I'm including a few links below.
And for getting the coordinates without GPS seems there aren't too much solutions, but there are some. This would be an example: How to get Latitute Longitude value without using GPS in Android?
More about this:
Reference on Google Cloud Messaging
Android Push Notifications using Google Cloud Messaging GCM - Android Example
Google Cloud Messaging using PHP
Connection between PHP (server) and Android (client) Using HTTP and JSON
Notificaciones Push Android: Google Cloud Messaging (GCM). Implementación Cliente (Nueva Versión) (spanish)
what is the best way to send phone(s) location from a mobile app to the server?
It depends on what sampling you need and what you plan to use it for.
WS is the way to go.
If you plan to store the data then for scalability I suggest a nonSQL approach (Firebase is a good suggestion).
Android: How to send location information to server while app is inactive
I would not use just a Service
, Better set an Alarm using AlarmManager
each (5 minutes, 15 minutes or what you need) and then get the location. @CommonsWare have done some code thats quite useful ( it deals also with WEAK_LOCKS
so if the phone goes sleep everything still run etc..)
Also, You don't need to use C2DM because you must send your location to the server and the server response you with the geo-information. C2DM is only for the case that you not start the communication.
Continuous sending location to server.. good or bad?
it is not good actually because it consumes a lot part of battery and also network. But as per your requirement it is good. Even i am also working on almost same concept of application but it is okay coz there are no another solutions so keep moving on that...
Android: How to periodically send location to a server
You need to create a separate class that is a subclass of the Service
class.
Service Documentation
Your primary application should can call startService
and stopService
to start up the background process. Theres also some other useful calls in the context class to manage the service:
Context Documentation
Most Recommended approach to send periodic location information to server continuously in Android?
The Android OS can kill a process at any time due to memory needs or other issues, so you have no guarantees that your process will continue to run. For similar reasons (limited resources on mobile devices), location services are optimized to reduce power consumption. One of these optimizations is to not provide location updates if you're not moving.
For a recommended approach, I'd go with this guy:
http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
Sending Location Updates to Server In Android
You should probably have your app register an alarm that wakes a service installed with your app on the child phone. This service should send location data to a server which will push it to the parent phone. The app will also need to install a service on the parent phone to receive notifications.
To decide which is the parent and which is the child you would most likely need to configure the app individually on each phone i.e. have the parent install the app on their phone and on the child phone and set the child phone as child and theirs as parent. You might password protect this setting. The other option would be to develop two apps, Where's My Kid (tracking) and Where's My Kid (parent).
Finally, you need a system running on a server to manage the data coming from the child. It would then send relevant information to the parent device. You might also have the parent create an account and then have them log in on the parent and child devices so that the server knows where to send the information.
This is not an easy project. If you are looking to build something commercial or distributive, it will require quite a bit of work. If it's really only a proof of concept as you mentioned, you can cut some corners. Either way, what I outlined is pretty much the minimum you will need.
Related Topics
Relocate Google Logo in Mapview
Android: How to Update a Imageview/Imagebutton with a Number to Show the Number of New Messages
Practical Way to Find Out If Sms Has Been Sent
Dagger 2 Injecting Android Application Context
Android Sdk Tools Revision 22 Issue
Onserviceconnected Never Called After Bindservice Method
Pop the Fragment Backstack Without Playing the Pop-Animation
Map Markers with Text in Google Maps Android API V2
Android Sharedpreferences , How to Save a Simple Int Variable
Launching an Android Application from the Browser
Get Current Visible Text in Textview
Android Sdk Content Loader Failing with Nullpointerexception