java - 将 GPS 坐标发送到另一部手机

标签 java android gps

我创建了一个应用程序,当我单击按钮时,它会向我提供 GPS 坐标。但我想,当我单击该按钮时,将它们发送到另一部安装了此应用程序的手机。我怎样才能做到这一点?非常感谢您提前的答复。

这是到目前为止我的代码: 这是 .java 文件:(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)
    {
    }
}
}

以及 .xml 文件(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>

此外,我已将其添加到 AndroidManifest.xml 文件中:

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

最佳答案

您需要在服务器的帮助下完成此操作,您可以做一件事。将您的设备ID以及GPS坐标发送到服务器,为此您需要创建一个网络服务。

整个事情就像下面这样。

假设您有两台设备,其中一台设备是 DeviceA,第二台设备是 DeviceB,当前如果您正在使用 DeviceA 并且您想要将GPS坐标发送到DeviceB,然后先获取设备A的设备ID,您可以找到任何Android设备的设备ID,如下所示

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

此方法将返回您的设备 ID。设备 ID 是每个 Android 应用程序的唯一标识。

获取设备 ID 后,您需要找到设备的 GPS 坐标。您可以按照以下步骤操作

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;
    }
}

此方法将为您提供您所在位置的纬度和经度...

现在您有了设备 ID 和 GPS 坐标这两个值,您可以将这两个值传递到您的网络服务器,然后从网络服务器将此数据传递到刚刚发送设备 ID 的设备。

好的,现在的问题是如何将 GPS 坐标获取到第二个设备,可以按如下方式完成。

创建一个单独的 Web 服务,用于通过设备 ID 获取 GPS 坐标,因此只需将第二个设备的设备 ID 传递给 Web 服务,作为返回,您将获得 GPS 数据。

我希望你清楚我的逻辑

祝你好运

关于java - 将 GPS 坐标发送到另一部手机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20391725/

相关文章:

android - React Native - 在一个屏幕上显示两个应用程序栏

iPhone Robotics Visual-SLAM算法实现

iOS:是否可以要求核心位置框架仅使用蜂窝塔或仅使用 WiFi 或使用 GPS 天线来获取位置?

java - ArrayList 类型不是通用的;它不能用参数 <Integer> 参数化

java - 如何在 groovy 中执行 url?

java - 在 Java 中从在线 URL 读取格式化数据到列表的最佳方法

java - 在 ASM 字节码中打印一个方法

android - HTTPS 请求返回 2032 Stream Error 仅适用于 Android with Flex 4.6 Mobile

java - 如何更正平滑移动谷歌地图 v2 中的标记?

java - 基本数据类型 准确性