为什么不是Android的onProviderEnabled()方法被调用? [英] Why isn't Android's onProviderEnabled() method being called?

查看:2384
本文介绍了为什么不是Android的onProviderEnabled()方法被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序有两个位置监听器,一个用于细一粗听。我希望能够检测何时用户接通其定位服务和关闭。

每当我把我的手机GPS或网络位置服务关闭的onProviderDisabled方法被调用预期。当我打开GPS或网络上的服务onProviderEnabled不会被调用!

我已经把$ C $下下面的听众之一......

更新...

我发现这个问题是关系到注销听众的onPause。当我删除了code locationManager.removeUpdates(myFineLocationListener);的onProviderEnabled()方法被调用

也许会自动禁止(说)GPS的行为注销监听器?也许我应该注销thje听者的onDestroy而不是在onPause()???

PS。也许这是有关我提到我的活动有一个按钮,直接进入设备的位置设置页面。它作为一个快捷方式。我用这个快捷方式来禁用/启用位置服务,然后使用后退按钮返回到我的应用程序。我会把$ C $下此键在这篇文章的最后。

code:

(该createLocListeners()方法从的onCreate()调用;)

 无效createLocListeners(){

        //如果为null,创建一个新的监听器
        // myFineLocationListener是一个类变量
        如果(myFineLocationListener == NULL){

        //倾听精确的位置信息更新
                myFineLocationListener =新LocationListener的(){

                    @覆盖
                    公共无效onLocationChanged(位置定位){
                        //做一点事
                    }

                    @覆盖
                    公共无效onProviderDisabled(字符串提供商){
                                 //这个被调用时,我改变手机上的位置设置(如GPS或关闭)}



                                  @覆盖
                           公共无效onProviderEnabled(字符串提供商){
                                  //这个时候我更改手机上的位置设置(如GPS或关闭)不会被调用
                           }

                    @覆盖
                    公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
                    }

                }; //在位置监听器端
        } // end如果细listener为null

    } //结束createLocListener()函数






    @覆盖
    保护无效onResume(){
        super.onResume();

        //寄存器地址听众
        尝试 {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,LISTENING_INTERVAL,LISTENING_DISTANCE,myFineLocationListener);
        }
        赶上(抛出:IllegalArgumentException E){
        //尝试捕捉异常,但没有发现任何
        }
        赶上(RuntimeException的E){
        }





    } //结束onResume()




    @覆盖
        保护无效的onPause(){
        super.onPause();

        // UNREGISTER位置监听器
        locationManager.removeUpdates(myFineLocationListener);

    } //结束的onPause
 

类似的问题: <一个href="http://stackoverflow.com/questions/6454990/location-service-onproviderenabled-never-called">Location服务onProviderEnabled从来没有所谓

$ C $下按钮,在打开的位置设置页面...

  //调用位置服务设置
    ibLoc.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            startActivity(新意图(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }

    }); //结束跳转到位置服务按钮监听器
 

解决方案

IMO,当你去设置的onPause()被调用,听众注册。然后,您启用GPS,并返回到你的活动。当您注册的GPS已经启用的听众,所以这样听者 onProviderEnabled()永远不会被调用,当GPS状态的修改为启用。

My Android app has two location listeners, one for fine and one for coarse listening. I want to be able to detect when the user turns their location services off and on.

Whenever I turn my phones GPS or network location services OFF the onProviderDisabled method is called as expected. When I turn GPS or network services ON the onProviderEnabled is never called!

I have put the code for one of the listeners below....

Update...

I have discovered that the problem is related to unregistering the listeners in onPause. When I remove the code "locationManager.removeUpdates(myFineLocationListener);" the onProviderEnabled() method IS called.

Perhaps the act of disabling the (say) GPS automatically unregisters the listener? Perhaps I should unregister thje listener in onDestroy rather than onPause() ???

PS. Maybe it is relevant that I mention that my activity has a button that goes directly to the devices location settings page. It acts as a shortcut. I use this shortcut to disable/enable location services then use the Back button to return to my app. I will put the code for this button at the very end of this post.

Code:

(The createLocListeners() method is called from onCreate();)

 void createLocListeners() {

        //if null, create a new listener
        //myFineLocationListener is a class variable     
        if (myFineLocationListener == null) {

        //Listen for FINE location updates   
                myFineLocationListener = new LocationListener(){

                    @Override
                    public void onLocationChanged(Location location) {
                        //Do something
                    }

                    @Override
                    public void onProviderDisabled(String provider) {   
                                 //This gets called when I change location settings (eg GPS on or off)  on the phone            }



                                  @Override
                           public void onProviderEnabled(String provider) {
                                  //This DOES NOT get called when I change location settings (eg GPS on or off)  on the phone   
                           }

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

                };  //end on location listener
        } //end if fine listener is null

    } //end createLocListener() function






    @Override
    protected void onResume() {
        super.onResume();

        //REGISTER LOCATION LISTENERS
        try {
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LISTENING_INTERVAL, LISTENING_DISTANCE, myFineLocationListener);
        } 
        catch (IllegalArgumentException e){
        //Tried catching exceptions but there weren't any
        }         
        catch (RuntimeException e) {            
        }





    }// end onResume()




    @Override     
        protected void onPause() {         
        super.onPause();        

        //UNREGISTER LOCATION LISTENERS
        locationManager.removeUpdates(myFineLocationListener);

    } //end onPause

Similar problem: Location service onProviderEnabled never called

Code for button that opens the location Settings page...

        //JUMP TO LOCATION SERVICES SETTINGS
    ibLoc.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
        }

    }); //end jump to location services button listener

解决方案

IMO, when you go to settings the onPause() is called and listeners are unregistered. Then you enable the GPS and return to your activity. So when you register the listeners the GPS is already enabled, so listener onProviderEnabled() is never called, since it is only called when GPS status is changed to enabled.

这篇关于为什么不是Android的onProviderEnabled()方法被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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