android - onlocationchanged 未在 Android 中调用

标签 android gps android-location locationlistener

我正在尝试使用 Android 中的 LocationListener 方法获取当前设备位置。在某些手机中,它不起作用或不会调用 onlocationchanged。在这种情况下我如何获得位置? 在 oncreate 中:

mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 0, this);
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

还有这个

@Override
    public void onLocationChanged(Location location) {

        latitude = location.getLatitude();
        longitude = location.getLongitude();
        Log.e("TAG","lat and long "+latitude+" "+longitude);
}

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

但是棉花糖以下版本不调用onLocationChanged

最佳答案

使用此代码适用于所有设备并经过测试:

 private void getCurrentLocation() {
//        Log.e("getLocation","Called");

        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                mlat = location.getLatitude();
                mlon = location.getLongitude();
       String mLogn = Double.toString(mlon);
               String mLatd = Double.toString(mlat);
//                Log.e("mLogn",""+mLogn);
//                Log.e("mLatd",""+mLatd);
                mLogn=mLogn.trim();
                mLatd=mLatd.trim();
                if (mLatd==null || mLogn==null) {
                    if (mLatd.isEmpty()) {
                        mLatd = "No data found";
                    }
                    if (mLogn.isEmpty()) {
                        mLogn = "No data found";
                    }
                }

                // Log.e("Location",""+mlon+" "+mlat);
//                Toast.makeText(CampaignTrain.this, "your location is " + mLogn + " " + mLatd, Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {

            }
        };
        String locationProvide = LocationManager.NETWORK_PROVIDER;
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},COARSE_LOCATION_REQUEST_CODE);
                return;
            }


        }
        if ( ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},FINE_LOCATION_REQUEST_CODE);
                return;
            }

        }
        locationManager.requestLocationUpdates(locationProvide, 0, 0, locationListener);
        Location lastLocation=locationManager.getLastKnownLocation(locationProvide);


    }

并设置运行时权限:

int FINE_LOCATION_REQUEST_CODE=101;
int COARSE_LOCATION_REQUEST_CODE=102;

     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode)
            {

               case COARSE_LOCATION_REQUEST_CODE:
                {
                    if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
                    {

                    }
                    else
                    {
                        mLatd="User denied location";
                        mLogn="User denied location";
                    }
                    return;
                }
                case FINE_LOCATION_REQUEST_CODE:
                {
                    if (grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED)
                    {

                    }
                    else
                    {
                        mLatd="User denied location";
                        mLogn="User denied location";
                    }
                    return;
                }
            }
        }

关于android - onlocationchanged 未在 Android 中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38736584/

相关文章:

android - 使用 ImageLoader 显示带有外部图像链接的通知

android - distance To() 返回的距离不是米!

当用户离开家时android禁用wifi

android - 监控 GPS 状态(如果用户使用通知栏关闭 GPS 则无法检查)

android - 单击 TextView Android中的特定单词时如何从字符串中提取单词

android - Fragments 有效地替换 Fragments 和 Navigating Fragments

android - 如何在android中发送关于我的位置地址的短信

java - 降低复杂性。查找最接近的纬度和经度对

ios - 从后台重新启动位置更新

java - 如何在 Android 中使用 Wifi 获取位置信息?