获取位置不适用于Android

时间:2017-11-22 18:12:31

标签: android

使用此代码我试图获取位置此代码仅在我第一次执行模拟器时工作,我必须关闭模拟器以便再次工作,并且它不会启动任何错误

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insert);
        myContext = this;
        position =(TextView)findViewById(R.id.position);
        locationManager=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);


        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(android.location.Location location) {
                double latitude=location.getLatitude();
                double longitude=location.getLongitude();
                position.setText("lat:"+latitude+" lon:"+longitude);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    101);
        }
        else{
            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(location != null){

            }
            else{
                locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        0,
                        0, locationListener);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        try {
            switch (requestCode) {
                case 101: {
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                    } else {
                    }
                    return;
                }
            }
        }
        catch(SecurityException err_permission) {
        }
    }

}

以及manifest.xml上的这个权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

但是这个位置没有显示在textview上,我想我没有错过任何东西,是吗?也许是rough_location权限?我不知道这个代码有什么问题,它有locationmanager和listener,并且aks表格许可......

2 个答案:

答案 0 :(得分:0)

如果locationManager.getLastKnownLocation中有最后一个已知位置,则您不会请求位置更新。

答案 1 :(得分:0)

       Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location != null){

        }
        else{
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    0,
                    0, locationListener);
        }

如果您的位置没有开始定期更新,您可能正在寻找:

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    0,
                    0, locationListener);
        if(location != null){
              //Do things with the first location
        }

所以你从一些东西开始并继续用监听器进行更新