设置 GPS 监听超时

Posted

技术标签:

【中文标题】设置 GPS 监听超时【英文标题】:set Timeout for GPS listening 【发布时间】:2012-10-07 07:59:45 【问题描述】:

我可以从网络提供商处获取位置更新,但在 GPS 方面,需要花费大量时间才能获取数据。我想保留一个只有 GPS 监听器才能工作的特定时间,然后在一段时间后转到网络提供商。如何解决这个问题?

这是我的代码..

public void gpslocation()
    
        final LocationManager locationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() 

    public void onLocationChanged(Location location)

    
        updateLocationForGeo(location);
        //update(location);
        // Called when a new location is found by the network location provider.
        makeUseOfNewLocation(location);

    

    private void makeUseOfNewLocation(Location location) 
        // TODO Auto-generated method stub

    

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

    public void onProviderEnabled(String provider) 

        System.out.println(provider+ "enabled  provider");

       


    public void onProviderDisabled(String provider) 
        // TODO Auto-generated method stub
        System.out.println(provider+ "disabled provider");
        networklocation();



    

        ;


    String locationProvider = LocationManager.GPS_PROVIDER;
    locationManager.requestLocationUpdates(locationProvider,  10 * 1000, (float) 10.0,locationListener);


            
    public void networklocation()
    
        final LocationManager locationManager = (LocationManager) this
        .getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() 

    public void onLocationChanged(Location location)

    
        updateLocationForGeo(location);
        //update(location);
        // Called when a new location is found by the network location provider.
        makeUseOfNewLocation(location);

    

    private void makeUseOfNewLocation(Location location) 
        // TODO Auto-generated method stub

    

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

    public void onProviderEnabled(String provider) 

        System.out.println(provider+ "enabled  provider");

       


    public void onProviderDisabled(String provider) 
        // TODO Auto-generated method stub
        System.out.println(provider+ "disabled provider");
        isGpsProvidersDisabled=true;




    

        ;



    String timeProvider = LocationManager.NETWORK_PROVIDER;
    locationManager.requestLocationUpdates(timeProvider,  1 * 1000, (float) 10.0, locationListener);

            
public void updateLocationForGeo(Location location)

    System.out.println("location updated");
    double dev_lat = location.getLatitude();
    double dev_lang = location.getLongitude();
    boolean out_of_range=false;

    for(int i=0; i<arrayLength; i++)

        double lattDiff = Math.toRadians(latarr[i]-dev_lat);

         double longDiff =  Math.toRadians(lonarr[i]-dev_lang);

         double distance=(Math.sin(lattDiff/2)*Math.sin(lattDiff/2))+(Math.sin(longDiff/2)*Math.sin(longDiff/2)*Math.cos( Math.toRadians(latarr[i]))*Math.cos( Math.toRadians(dev_lat)));

         System.out.println(distance+"  distance" );
         double c= (2 * Math.atan2(Math.sqrt(distance), Math.sqrt(1-distance)));



             double radius=radarr[i]* 1.60934;



             double d = 6371 * c;


         if(d>radius)
            

        out_of_range=true;  
            continue;

            
        else
            System.out.println("enjoy");
            out_of_range=false;
            break;
            

    

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
        mlocListener);

public class MyLocationListener implements LocationListener 
private Address mAddresses;
@Override
public void onLocationChanged(Location loc) 
    loc.getLatitude();
    loc.getLongitude();

    Geocoder gcd = new Geocoder(getApplicationContext(),
            Locale.getDefault());
    try 
        mAddresses = gcd.getFromLocation(loc.getLatitude(),
                loc.getLongitude(), 1);

     catch (IOException e) 

    

    String cityName = (mAddresses != null) ? mAddresses.get(0)
            .getLocality() : TimeZone.getDefault().getID();
    String countryName = (mAddresses != null) ? mAddresses.get(0)
            .getCountryName() : Locale.getDefault().getDisplayCountry()
            .toString();


    mCurrentSpeed.setText("Longitude"+loc.getLongitude()+" Latitude"+loc.getLatitude());


@Override
public void onProviderDisabled(String provider) 
    Toast.makeText(getApplicationContext(), "Gps Disabled",
            Toast.LENGTH_SHORT).show();


@Override
public void onProviderEnabled(String provider) 
    Toast.makeText(getApplicationContext(), "Gps Enabled",
            Toast.LENGTH_SHORT).show();


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

 

【讨论】:

实际上 io 能够读取网络提供商何时可用但只有 gps 没有任何事情发生任何帮助如何检查 gps 是否工作将更有帮助 如果 Gps 信号强度良好,您可以每秒接收更新,并且无论何时位置发生变化。您的气候条件可能是第一个原因,其次您的设备 GPS 功能可能非常低。这是您无法获得 GPS 信号的两个原因 是的,没错,但我想知道我是否可以将计时器保持 2 分钟,如果 gps 不在该时间内定位,请移至网络提供商。如果可能的话,我将计时器放置在哪里即在哪个功能中(启用了提供程序,onlocatin 更改,onstatuschanged 等)? 哦,好的,你能给我一些建议吗? 查看如何在android中实现Timer类

以上是关于设置 GPS 监听超时的主要内容,如果未能解决你的问题,请参考以下文章

检查超时监听器

Android如何监听GPS状态变化?

GPS定位研究之gps位置改变监听源码分析(Android10)

sqlplusrac连接超时

iOS NSURLSession 监听超时

使用 node-postgres 监听查询超时?