LiDaming_and 2013-03-15 01:58 采纳率: 100%
浏览 2216
已采纳

在接收到经度和纬度时,如何停止GPS?

我做了一个 geolocation 程序,该程序能正常运行,但总是循环持续显示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);

那么它就会只运行一次?

  • 写回答

2条回答 默认 最新

  • yongyong_21 2013-03-18 09:20
    关注

    当你想停止监听器时,你可以添加

    locationManager.removeUpdates(locationListener); 
    locationManager = null;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路