程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> java-在接收到經度和緯度時,如何停止GPS?

java-在接收到經度和緯度時,如何停止GPS?

編輯:編程綜合問答
在接收到經度和緯度時,如何停止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);

那麼它就會只運行一次?

最佳回答:


當你想停止監聽器時,你可以添加

locationManager.removeUpdates(locationListener); 
locationManager = null;
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved