我正在創建一個使用GPS的應用程序。我第一次在onCreate()
我正在檢查GPS是否啓用。如果它未啓用,則我將用戶發送到設置菜單以設置GPS已啓用。如何在android中禁用用戶的gps時得到通知?
GPS啓用後,我開始執行我的東西。 但是,如果在用戶之間停止通知管理器上的GPS滾動,那麼我怎麼會知道這一點?
因爲我的方法只在GPS未啓用時執行一次。之後,它再次被禁用時不會執行。
I just want to know whenever in between the work the GPS is disabled by the user. Just this information is required.
這裏是我的代碼。
class LocationFinderService extends Service{
boolean gps_enabled;
LocationManager locationManager;
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
if(!isGpsEnabled())
{
startGpsForLocation();
stopSelf();
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
public boolean isGpsEnabled()
{
gps_enabled = (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)&& locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER));
Log.i("Log", "Gps: "+gps_enabled);
return gps_enabled;
}
public void startGpsForLocation()
{
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) ;
startActivity(intent);
Toast.makeText(this, "Please start the GPS", Toast.LENGTH_LONG).show();
}
}
我呼籲Activity類的按鈕,點擊此服務。
任何幫助,將不勝感激。
可能重複[Android的 - 有沒有辦法聽,如果GPS已啓用或禁用(http://stackoverflow.com/questions-7521509/android-is-there-a-way-to-listen-if-gps-was-enabled-or-disabled) –