[英]How to receive gps enabled/disabled?
嗨,我正在嘗試編寫一個小部件來顯示GPS狀態。 如果用戶更改GPS啟用狀態,我希望更新我的小部件。
換句話說: 當gps狀態更改時,我想更新我的小部件。 我需要寫一個BroadcastReceiver嗎? 如果是,它將采取什么行動?
感謝您的回答。
以下是我最近用來執行此操作的方法,但是我必須警告您,當根本沒有GPS硬件時,我發現它(至少在一台設備上)返回true。 傳遞一個類似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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.