Android 位置循环

Android location loop

我正在使用以下代码获取用户位置:

public class MainActivity extends ActionBarActivity {
  //=========================================== On Create ========================================
  @
  Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //======================================= Location =========================================
    LocationManager mlocManager =
      (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    LocationListener mlocListener = new MyLocationListener();
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
  }
}

public class MyLocationListener implements LocationListener {@
  Override
  public void onLocationChanged(Location loc) {
    loc.getLatitude();
    loc.getLongitude();
    String Text = "My current location is: " +
      "Latitude = " + loc.getLatitude() +
      "Longitude = " + loc.getLongitude();
    Toast.makeText(getApplicationContext(),
      Text,
      Toast.LENGTH_SHORT).show();
  }@
  Override
  public void onProviderDisabled(String provider) {
    Toast.makeText(getApplicationContext(),
      "Gps Disabled",
      Toast.LENGTH_SHORT).show();
  }@
  Override
  public void onProviderEnabled(String provider) {
    Toast.makeText(getApplicationContext(),
      "Gps Enabled",
      Toast.LENGTH_SHORT).show();
  }@
  Override
  public void onStatusChanged(String provider, int status, Bundle extras) {}
}

当我 运行 应用程序时,它会进入无限循环,向我显示用户的位置。我怎样才能让它通过点击按钮只显示一次位置?

编辑了下面的代码以使用 GPS 或网络位置。

同时使用这些权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

这是一个非常简单的例子,一个 UI 和一个 Button 和一个 TextView

MainActivity.java:

public class MainActivity extends ActionBarActivity {

Button button;
TextView locText;
double lat;
double lon;
Location loc;
LocationManager mlocManager;
String provider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button)findViewById(R.id.buttonGo);

    locText = (TextView)findViewById(R.id.textView);

    mlocManager =
            (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    //LocationListener mlocListener = new MyLocationListener();
    //mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

    Criteria criteria = new Criteria();
    //criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAccuracy(Criteria.ACCURACY_COARSE); //Try this instead
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);

    provider = mlocManager.getBestProvider(criteria, true);

    if (provider != null) {
        loc = mlocManager.getLastKnownLocation(provider);

        lat = loc.getLatitude();
        lon = loc.getLongitude();
    }

    locText.setText(lat + " " + lon);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (provider != null) {
                loc = mlocManager.getLastKnownLocation(provider);

                lat = loc.getLatitude();
                lon = loc.getLongitude();
            }

            locText.setText(lat + " " + lon);
        }
    });
}

activity_main.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=".MainActivity">

        <Button
            android:id="@+id/buttonGo"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Refresh Location"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/textView"
            android:layout_below="@+id/buttonGo"
            android:layout_marginTop="86dp" />

</RelativeLayout>