在 Android 中未调用 onlocationchanged

Posted

技术标签:

【中文标题】在 Android 中未调用 onlocationchanged【英文标题】:onlocationchanged not called in Android 【发布时间】:2016-12-08 18:16:26 【问题描述】:

我正在尝试使用 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 在 Marshmallow 以下的版本中不会被调用

【问题讨论】:

你能说明你是如何调用位置更新的吗? @PabloBaxter 我已经更新了代码 这是在 Marshmallow 以下的所有手机上,还是只有一部? @PabloBaxter 它在棉花糖手机中运行良好。 Lollipop 设备失败。我在 2 台设备上测试过 在您的位置设置中,您是否启用了 GPS? 【参考方案1】:

使用此代码在所有设备上工作并经过测试:

 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;
                
            
        

【讨论】:

它返回一个位置,但 onlocationchanged 给了我我的旧位置。我需要我当前的位置。 LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE) 在 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this) 下添加这一行;

以上是关于在 Android 中未调用 onlocationchanged的主要内容,如果未能解决你的问题,请参考以下文章

android广播接收器中未调用onReceive()进行静态广播

片段中未调用 onAttach()

Android:片段在活动结果合同中未收到 RESULT_OK

无法在 aynctask 中未调用 Looper.prepare() 异常的线程内创建处理程序

AppCompatActivity 中未解析的 Android Activity Results API 引用

如果显示插页式广告,则在活动中未调用onResume()和onPause()