[英]How to turn GPS on and off?
Android是否允許以編程方式打開和關閉GPS?
我正在使用下面的代碼[功能turnGPSon()和turnGPSoff()]( 從此處開始 )
我正在使用函數“ GPSStatus()”來檢測GPS是否打開。
兩個問題:
turnGPSoff()無法正常工作。 turnGPSon()能夠打開GPS(“我認為”,因為我可以在左上角的狀態圖標中看到GPS符號,但是控制中心[galaxy s3]中的GPS按鈕並未突出顯示,並且我得到了准確的定位)
GPSStatus()總是返回false-我在檢查正確的方法嗎?
我的目標只是以編程方式打開和關閉GPS嗎? (這是真的,Android的不允許GPS進行開啟和關閉程序?作為在答復中提到在這里 )
private boolean GPSStatus() {
ContentResolver contentResolver = getBaseContext().getContentResolver();
boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver, LocationManager.GPS_PROVIDER);
return gpsStatus;
}
public void turnGPSOn()
{
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", true);
this.sendBroadcast(intent);
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.contains("gps")){ //if gps is disabled
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
this.sendBroadcast(poke);
}
}
// automatic turn off the gps
public void turnGPSOff()
{
myLogger.displayMsgs("in turnGPSoff()", true, true);
Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
intent.putExtra("enabled", false);
sendBroadcast(intent);
}
重復,原始在這里
回答:
(您需要rootaccess才能完全控制,因此幾乎是不可能的)
原始答案:
要獲得SuperAdmin特權,您需要a)根用戶和b)設計安裝在/ system / app目錄中的小型存根應用程序。 作為上述所有示例,您可以從https://github.com/sms2000/GPSToggler中獲取我的GPSToggler。 它或多或少地起作用,並且可以滿足您的需求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.