安卓:检查是否GPS正在搜索,有固定或不使用 [英] Android: check if GPS is searching, has fix or is not in use

查看:153
本文介绍了安卓:检查是否GPS正在搜索,有固定或不使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有在安卓(姜饼)的方式来知道此刻的GPS是做一些与否。让我更清楚一点:我基本上要调用一些方法或API,它会告诉我wheter的GPS是:

I want to know if there is a way in Android (GingerBread) to know if at the moment the GPS is doing something or not. Let me be a bit more clear: I basically want to call some method or api that will tell me wheter the GPS is:

1)固定(在状态栏图标GPS)
2)在搜索修复(在状态条上闪烁的GPS图标)
3)非活动(无应用程序使用定位服务的那一刻,在状态条上无图标)

1)Fixed (GPS icon in statusbar on)
2)Searching for fix (GPS icon on statusbar blinking)
3)Inactive (No app is using location services at the moment, no icon on statusbar)

现在:我知道,你可以使用一个LocationListener的这种变化的通知,但这不适合我,因为我不希望我的code保持运行等待等待事件,我的code定期在预定的时间运行,做一些事情,然后终止,所以我需要一种方法来检查GPS服务的状态在precise的时刻,而不是等待,当它改变的通知。

Now: I know that you can use a LocationListener to be notified of such changes BUT this is not good for me because I don't want my code to remain running waiting waiting for events, my code runs periodically at scheduled times, does something and then terminates, so I need a way to check the status of the GPS service in that precise moment, rather than wait for notifications of when it changes.

推荐答案

做大量的测试上GPS后,我终于找到了解决办法。当机器人应用程序调用位置管理器和GPS开始搜索,一个事件被触发,并且还当全球定位系统被锁定另一事件触发。继code展示了如何做到这一点。

After doing lot of testing on GPS, finally I found the solution. When android app calls location manager and GPS starts searching, one event is triggered and also when gps is locked another event is triggered. Following code shows how to do this.

    locationManager = (LocationManager)mContext.getSystemService(LOCATION_SERVICE);
    isGPSEnabled = locationManager
            .isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (isGPSEnabled) 
    {                   
        if (locationManager != null) 
        {

            // Register GPSStatus listener for events
            locationManager.addGpsStatusListener(mGPSStatusListener); 
            gpslocationListener = new LocationListener() 
            {
                public void onLocationChanged(Location loc) 
                {                                                           
                }
                public void onStatusChanged(String provider, int status, Bundle extras) {}
                public void onProviderEnabled(String provider) {}
                public void onProviderDisabled(String provider) {}                          
            };  

                           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                MIN_TIME_BW_UPDATES_GPS, MIN_DISTANCE_CHANGE_FOR_UPDATES_GPS,
                gpslocationListener);           
        }
    }

/ *      当各种事件发生时,如*这是GPSListener函数调用      * GPS启动,停止GPS,GPS锁定      * /

/* * This is GPSListener function invoked when various events occurs like * GPS started, GPS stopped, GPS locked */

public Listener mGPSStatusListener = new GpsStatus.Listener()
{    
    public void onGpsStatusChanged(int event) 
    {       
        switch(event) 
        {
            case GpsStatus.GPS_EVENT_STARTED:
                Toast.makeText(mContext, "GPS_SEARCHING", Toast.LENGTH_SHORT).show();
                System.out.println("TAG - GPS searching: ");                        
                break;
            case GpsStatus.GPS_EVENT_STOPPED:    
                System.out.println("TAG - GPS Stopped");
                break;
            case GpsStatus.GPS_EVENT_FIRST_FIX:

                /*
                 * GPS_EVENT_FIRST_FIX Event is called when GPS is locked            
                 */
                    Toast.makeText(mContext, "GPS_LOCKED", Toast.LENGTH_SHORT).show();
                    Location gpslocation = locationManager
                            .getLastKnownLocation(LocationManager.GPS_PROVIDER);

                    if(gpslocation != null)
                    {       
                    System.out.println("GPS Info:"+gpslocation.getLatitude()+":"+gpslocation.getLongitude());

                    /*
                     * Removing the GPS status listener once GPS is locked  
                     */
                        locationManager.removeGpsStatusListener(mGPSStatusListener);                
                    }               

                break;
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
 //                 System.out.println("TAG - GPS_EVENT_SATELLITE_STATUS");
                break;                  
       }
   }
};  

这是更好地把GPS code并作为服务来获取GPS位置信息。

It is better to put GPS code in and as service to get GPS location information.

每个如果调用GPS功能时,GPSStatus侦听器注册。 GPS_SEARCHING敬酒来,只有当GPS开始搜索和GPS_LOCKED敬酒时显示GPS锁定一次。如果我们再次调用GPS功能,GPS_EVENT_FIRST_FIX事件被触发,如果它被锁定(显示GPS_LOCKED吐司),如果GPS已经开始寻找它不会显示GPS_SEARCHING吐司(即GPS_STARTED事件不会触发)。后GPS_EVENT_FIRST_FIX事件被触发的即时去除GPSstatus监听器的更新。

Each time if you call GPS function, GPSStatus listener is registered. GPS_SEARCHING toast comes only once when GPS is started to search and GPS_LOCKED toast displays when GPS is locked. If we call GPS function again, GPS_EVENT_FIRST_FIX event is triggered if it is locked(displays GPS_LOCKED toast) and if GPS is already started to search it won't display GPS_SEARCHING toast(i.e GPS_STARTED event won't trigger). After GPS_EVENT_FIRST_FIX event is triggered i'm removing the GPSstatus listener updates.

当GPS_EVENT_FIRST_FIX事件被触发,它能够更好地调用gpslastknownlocation()函数来获取新鲜最新的GPS定位。(其更好地寻找到Android开发者网站获取更多信息)。

When GPS_EVENT_FIRST_FIX event is triggered, its better to call gpslastknownlocation() function to get fresh latest GPS fix.(Its better to look into Android developers site for more info).

我希望这将帮助别人......

I hope this will help others....

这篇关于安卓:检查是否GPS正在搜索,有固定或不使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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