java - 收到一次经纬度后如何停止这个GPS?

标签 java android android-manifest

我制作了一个地理定位应用程序..它工作成功,但总是循环并连续显示toast..这是代码:

package com.application.geocoding;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.widget.TextView;
import android.widget.Toast;

public class main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        geoLocation();

    }
    public void geoLocation()
    {
        LocationManager locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
              // Called when a new location is found by the network location provider.
              //makeUseOfNewLocation(location);
                updateWithNewLocation(location);
            }

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

            public void onProviderEnabled(String provider) {
                Toast.makeText(main.this,"Network Enabled",Toast.LENGTH_SHORT ).show();
            }

            public void onProviderDisabled(String provider) {
                Toast.makeText(main.this,"Network Disabled",Toast.LENGTH_SHORT ).show();
            }
          };

        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    }

    public void updateWithNewLocation(Location location) {
        String latLongString,addressString = "Location not found";
        TextView myLocationText,myLocationText2; 
        myLocationText=(TextView)findViewById(R.id.textView1);
        myLocationText2=(TextView)findViewById(R.id.textView2);

        if (location != null) {
          double lat = location.getLatitude();
          double lng = location.getLongitude();

          Geocoder gc = new Geocoder(this, Locale.getDefault());
          try {
            List<Address> addresses = gc.getFromLocation(lat, lng, 1);
            StringBuilder sb = new StringBuilder();
            if (addresses.size() > 0) {
              Address address = addresses.get(0);

              for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                sb.append(address.getAddressLine(i)).append("\n");

                sb.append(address.getLocality()).append("\n");
                //sb.append(address.getPostalCode()).append("\n");
                sb.append(address.getCountryName());
            }
            addressString = sb.toString();
          } catch (IOException e) {}

          latLongString = "Lat:" + lat + "\nLong:" + lng;
        } else {
          latLongString = "No location found"; 
        }
        //myLocationText.setText("Your Current Position is:\n" + 
                               //latLongString);
        //myLocationText2.setText(addressString);
        Toast.makeText(this, latLongString, 1).show();
        Toast.makeText(this, addressString, 1).show();
    }

}

有谁知道我应该把它放在哪里:

 locationManager.removeUpdates(locationListener);

所以它只会运行一次?谢谢..

最佳答案

你可以放

locationManager.removeUpdates(locationListener); 
locationManager = null;

无论你想在哪里停止监听器。

关于java - 收到一次经纬度后如何停止这个GPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6965259/

相关文章:

android - 推送通知以获取设备位置

android - 目标和最低 Android 版本支持平板电脑

java - 如何比较 XML 枚举和 Java 枚举?

java - JAXB 将分组元素添加到列表中,类似于 .NET

java - 在 Windows 10 上安装 Storm

android - R.java android中常量的方法

java - 如何在 Java EE 应用程序中使用多线程环境

android - 无法在 Flutter 中构建 .aab

android - 就我而言,如何将一种布局嵌入另一种布局?

android - 在项目或库中找不到 list 中引用的类 io.flutter.app.FlutterApplication