使用 Gps 获取纬度和经度

using Gps Getting Latitude and Longitude

请任何人给我代码, { 每 5 分钟使用 gps 获取经纬度, 并使用 Json 解析

将该纬度和经度发送到 Web url

如果您关闭该应用程序,我们也可以获得纬度和经度

}

http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

转到此 link 获取您的位置

您可以使用 postdelay 处理程序实现一个循环,以实现 5 分钟的刷新率和 运行 里面的 getLocation...

使用 SharedPreferenceManager 将最后已知的纬度和经度存储到 sdcard 数据

https://mongskiewl.wordpress.com/2013/10/16/sending-json-data-from-android-to-a-php-script/

按照此发送 JSON 数据 :)

如果你想在你的应用程序未打开的情况下使你的代码 运行,你需要 services

一个service 运行在后台,如果你想要纬度和经度的详细信息,你需要一个LocationListener

如果您需要您的应用程序每 3 分钟发送一次详细信息,您将需要设置一个 timer 并获取纬度和经度,然后使用 JSON.[=20= 发送它]

如何创建服务

清单文件

<service
  android:name="MyService"
  android:icon="@drawable/icon"
  android:label="@string/service_name"
  >
</service> 

服务java文件

    public class MyService extends Service {

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    //TODO do something useful
      //write your repetition code here.
    return Service.START_NOT_STICKY;
  }

  @Override
  public IBinder onBind(Intent intent) {
  //TODO for communication return IBinder implementation
    return null;
  }
} 

调用您的服务

Intent i= new Intent(context, MyService.class);
// potentially add data to the intent
i.putExtra("KEY1", "Value to be used by the service");
context.startService(i); 

获取经纬度

  @Override
  public void onLocationChanged(Location location) {
    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
  }

因此,我建议您 运行 一个后台服务,它可以定期获取纬度和经度的详细信息,然后使用 JSON.

发送它