如何启用/禁用gps?

时间:2011-03-01 17:36:21

标签: android gps listener broadcastreceiver

您好我正在尝试编写一个显示GPS状态的小部件。 如果用户更改GPS启用状态,我希望我的小部件更新。

换句话说:我想在gps状态发生变化时更新我的小部件 。 我需要写一个BroadcastReceiver吗?如果是,它将收到什么行动?

感谢您的回答。

2 个答案:

答案 0 :(得分:0)

以下是我最近用来做这个的方法,但我必须警告你,我发现它(至少在一台设备上)在没有GPS硬件的情况下才会返回。传递一个像LocationManager.GPS_PROVIDER这样的值。 locationManager是LocationManager的一个实例。希望你会发现这段代码很有用,但是在我目前的场景中它虽然适合我,但我希望你需要改进它。

// Checks whether the specified location provider is enabled on the device
public static boolean isLocationProviderEnabled(String provider)
{
    if ( locationManager.isProviderEnabled(provider) )
    {
        if (provider.toLowerCase().equals(LocationManager.GPS_PROVIDER))
        {
            return haveGPS();
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }
}

// Checks whether the GPS hardware is present in the device
private static boolean haveGPS()
{
    PackageManager pmgr = SpondleApplication.getInstance().getPackageManager();
    boolean gotGPS = pmgr.hasSystemFeature("android.hardware.location.gps");
    return gotGPS;
}

答案 1 :(得分:0)