android에서 지리적 위치를 가져올 때null

8925 단어
많은 안드로이드 핸드폰이 지리적 위치를 얻는 것을 발견했다. 많은 경우에 반드시 얻을 수 없고 정확하지 않다. 왜냐하면 그 위치는 지난번에 얻었을 때 얻은 것이기 때문에 업데이트를 해야 한다.구체적인 실현 코드는 다음과 같다.
import android.app.Activity;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.widget.TextView;

import java.util.Iterator;
import java.util.List;

public class LocationBase extends Activity {
    private TextView myText;
    private String textStr = "";
    private LocationManager mgr = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addText("Location Based-Services Demo...");

        //   1:           
        mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
        addText("get system Location Manager " + mgr);
    }

    //  2.2         update:    ,            ,         removeUpdates(),          Activity   ,App            ,           
    protected void onPause() {
        super.onPause();
        PackageManager pm = getPackageManager();
        boolean permissionFine = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.ACCESS_FINE_LOCATION", getPackageName()));
        boolean permissionCoarse = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.ACCESS_COARSE_LOCATION", getPackageName()));
        if (permissionFine == true && permissionCoarse == true) {
            mgr.removeUpdates(onLocationChange);
        }
        addText("Remove Updates...");
    }

    //  2:      
    //   2.1(1) :   onResume()     ,    onPause()       。    Activity       。   Activity     
    protected void onResume() {
        super.onResume();


        mgr.addTestProvider(LocationManager.NETWORK_PROVIDER,

                "requiresNetwork" == "", "requiresSatellite" == "", "requiresCell" == "", "hasMonetaryCost" == "",
                "supportsAltitude" == "", "supportsSpeed" == "",
                "supportsBearing" == "", android.location.Criteria.POWER_LOW,
                android.location.Criteria.ACCURACY_FINE);
        mgr.setTestProviderEnabled(LocationManager.NETWORK_PROVIDER, true);

        PackageManager pm = getPackageManager();
        boolean permissionFine = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.ACCESS_FINE_LOCATION", getPackageName()));
        boolean permissionCoarse = (PackageManager.PERMISSION_GRANTED == pm.checkPermission("android.permission.ACCESS_COARSE_LOCATION", getPackageName()));

        addText("Request Updates automatically ...");

        List list = mgr.getAllProviders(); //mgr LocationManager
        for (Iterator i = list.iterator(); i.hasNext(); ) {
            System.out.println("\t" + i.next());
        }

        Criteria criteria = new Criteria();
        String providerName = mgr.getBestProvider(criteria, true /*enabledOnly*/); //criteria   null,      
        LocationProvider provider = mgr.getProvider(providerName);
        location = mgr.getLastKnownLocation(providerName);
        if (permissionFine == true && permissionCoarse == true) {
//  2.1 (2):           ,             ,                     ,           ,          。             ,   requestLocationUpdates (long minTime, float minDistance, Criteria criteria,PendingIntent intent),            Activity。     ,     GPS,               。
           while(location == null) {
            mgr.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100/*10 ,     */, 0.1f/*1  */, onLocationChange/*     */);
           }
        }

        if (location != null)
            System.out.println("Get Location from " + providerName + " : " + location.getLatitude() + "," + location.getLongitude());
    }

    Location location;

    //  3:     LocationListener    ,          onLocationChanged( )
    LocationListener onLocationChange = new LocationListener() {
        public void onLocationChanged(Location location2) {
            location = location2;
            addText("Location Changed : (" + location.getLongitude() + "," + location.getLatitude() + ")");
        }

        public void onProviderDisabled(String arg0) {
            addText("onProviderDisabled");
        }

        public void onProviderEnabled(String arg0) {
            addText("onProviderEnabled");
        }

        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
            addText("onStatusChanged");
        }
    };

    private void addText(String s) {
        textStr = textStr.concat(s + "

"
); myText.setText(textStr); } }

좋은 웹페이지 즐겨찾기