user3063909
user3063909

Reputation: 383

sending gps coordinates to another phone

I've created an application in which, when I click a button, it gives me my gps coordinates. But I want, when I click that button, to send them to another phone which has this application installed. How can I do this? Thank you very much for your answers in advance.

Here is my code so far: This is the .java file: (Saver.java):

package com.example.lifesaver;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class Saver extends Activity {

Button b;

Location newLocation = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_saver);
b = (Button) findViewById(R.id.button1);

b.setOnClickListener(new OnClickListener(){
  public void onClick(View v)
  {
      if (newLocation != null) {

          String Text = "Current location is: " + "Latitud = "
                  + newLocation.getLatitude() + "Longitud = "
                  + newLocation.getLongitude();
          Toast.makeText(getApplicationContext(), Text,
                  Toast.LENGTH_SHORT).show();
      }
  }
}); 


// We use LocationManager class to obtain GPS locations
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}

//MyLocationListener class

public class MyLocationListener implements LocationListener
{

    public void onLocationChanged(Location loc)
    {
        newLocation = loc;
    }

    public void onProviderDisabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
    }

    public void onProviderEnabled(String provider)
    {
        Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras)
    {
    }
}
}

And the .xml file(activity_saver.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Saver" >

<Button
android:id="@+id/button1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/circle" 
android:onClick="onClick"/>

</RelativeLayout>

Also, I've added this in the AndroidManifest.xml file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION">
</uses-permission>

Upvotes: 0

Views: 3140

Answers (4)

basitj
basitj

Reputation: 363

You can use one of the following to send these coordinates:

  1. Send a text message (SMS) to the other phone.
  2. Send it in an email.
  3. Send it using a server (tell the server, and then server tells the other phone)
  4. You can use a third party messaging service (Others)

So once you decide what you want to use for communication we can help you further.

Upvotes: 0

Kalpesh
Kalpesh

Reputation: 1807

As per I thinking you can use Google Cloud messaging for that functionality to work smooth if device are near or anywhere. You can check demo here for that: http://www.androidhive.info/2012/10/android-push-notifications-using-google-cloud-messaging-gcm-php-and-mysql/

Upvotes: 0

Aamirkhan
Aamirkhan

Reputation: 5794

You need to do it with help of server,You can do one thing.Send your Device ID along with GPS Cordinates to server,For that you need to create one webservice.

Entire thing is like below.

Suppose you have two devices one device is DeviceA & secound device is DeviceB,Currently if you are using DeviceA and if you want to send GPS Cordinates to DeviceB than first take Device Id of Device A,You can find device id of Any android Device as below

public static String deviceId(Context c){
        TelephonyManager tManager = (TelephonyManager)c.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = tManager.getDeviceId();
        return deviceId;
    }

This method will give you your device Id in return.Device id is unique Identification for each android application.

After getting device id you need to find GPS cordinates of your device.You can do it as below

private void _getLocation() {
    // Get the location manager
    LocationManager locationManager = (LocationManager) 
            getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String bestProvider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(bestProvider);
    try {
        lat = location.getLatitude();
        lon = location.getLongitude();
    } catch (NullPointerException e) {
        lat = -1.0;
        lon = -1.0;
    }
}

This method will give you latitude and longitude of your location...

So,now you have both values Device ID & GPS Cordinates,You can pass this two values to your webserver,and from web server pass this data to that device which device id is just sent.

Ok,now question is how will you get GPS Cordinates to secound device,it can be done as below.

Make one seperate webservice for getting GPS Cordinates by device id,so just simply pass device id of secound device to webservice and in return you will get GPS data.

I hope you are clear with my logic

Good luck

Upvotes: 1

insomniac
insomniac

Reputation: 11766

You need to either use a Socket connection or have to send it to a webservice which both of the phones can have access,if the phones are near use a Bluetooth socket, like in this tutorial http://digitalhacksblog.blogspot.in/2012/05/android-example-bluetooth-simple-spp.html

http://myandroidsolutions.blogspot.in/2012/07/android-tcp-connection-tutorial.html

Upvotes: 0

Related Questions