getlastknownlocation总是返回空后,我重新安装通过Eclipse的apk文件 [英] getlastknownlocation always return null after I re-install the apk file via eclipse

查看:192
本文介绍了getlastknownlocation总是返回空后,我重新安装通过Eclipse的apk文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我创建了一个简单的应用程序来获取GPS位置,显示屏上的Andr​​oid手机。 在开始的时候我能拿到的位置之后几尝试,但我后重新安装apk文件,该getLastKnownLocation()总是返回空值。

Recently, I created a simple application to get the gps location and display on android phone. At beginning I able to get the location after few try, but after i re-install the apk file, the getLastKnownLocation() always return a null value.

开发环境中使用: - API 10姜饼2.3.6 - 全球定位系统提供商使用

Development environment used: - API 10 gingerbread 2.3.6 - GPS provider is used

下面是我的Andr​​oid项目中应用的codeI:

below is the code i applied in my android project:

        public class MyActivity extends MapActivity{
protected void onCreate(Bundle savedInstanceState) {


    mapView = (MapView)findViewById(R.id.myTripMap);
    mapController = mapView.getController();
    mapView.setSatellite(false);
    mapView.setStreetView(true);
    mapView.displayZoomControls(false);
    mapView.setBuiltInZoomControls(true);//
    mapView.setClickable(true);
    mapController.setZoom(14);      


    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);
    criteria.setBearingRequired(false);
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true);
    location = locationManager.getLastKnownLocation(provider);

    updateMyCurrentLoc(location);

    locationManager.requestLocationUpdates(provider, 2, 1,locationListener);



}


          private final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          updateMyCurrentLoc(location);
        }

        public void onProviderDisabled(String provider){
          updateMyCurrentLoc(null);
        }

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


      private void updateMyCurrentLoc(Location location) {



            if (location != null) {

             // other codes to get the address and display
            Toast.makeText(getBaseContext(), "provider used : "+provider).show();   //testing purpose
            } 
            else {
              str = "No location found";
              Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
            }

        }
    }

任何人都可以提出一个可能的解决方案来解决getLastKnownLocation返回空值()? 任何帮助将AP preciated。谢谢你。

Can anyone suggest a possible solution to solve the null value returned by getLastKnownLocation()? Any helps will be appreciated. Thanks.

推荐答案

getLastKnownLocation()此返回最后一个已知位置......你以后重新安装应用程序它不会有任何一个已知位置......所以这将导致NPE ...而不是使用下面code

the getLastKnownLocation() this returns the last known location...after you re-install the app it will not have any last known location...so it will result in NPE...instead use below code

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

它的工作对我来说......应该为你工作太...

It worked for me...should work for you too...

这篇关于getlastknownlocation总是返回空后,我重新安装通过Eclipse的apk文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆