2016-05-06 1 views

ответ

0

Попробуйте использовать ниже код

public void turnGPSOn() 
{ 
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
    intent.putExtra("enabled", true); 
    this.ctx.sendBroadcast(intent); 

    String provider = Settings.Secure.getString(ctx.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.ctx.sendBroadcast(poke); 


    } 
} 
// automatic turn off the gps 
public void turnGPSOff() 
{ 
    String provider = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
    if(provider.contains("gps")){ //if gps is enabled 
     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.ctx.sendBroadcast(poke); 
    } 
} 

Вы можете включить GPS выключен по LocationSensor.Enabled = False

Вы можете прочитать учебник LocationSensor здесь: http://appinventor.mit.edu/explore/ai2/location-sensor.html

+0

Благодарим вас. Можно ли также поворачивать другие датчики? –

+0

Какие еще датчики вы хотите отключить? – Krishna

2

Включить GPS

Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE"); 
intent.putExtra("enabled", true); 
sendBroadcast(intent); 

Отключить GPS

Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); 
intent.putExtra("enabled", false); 
sendBroadcast(intent); 
+0

[Этот ответ] (http://stackoverflow.com/questions/22528984/android-device-gps-on-off-programatically#22529296) предполагает, что это не работает в современных версиях Android. Я думаю, что в настоящее время действия пользователя необходимы для изменения состояния GPS. –

+1

Спасибо, дорогой ... Можно ли отключить другие датчики? –

+0

да можно отключить, как Wi-Fi, Bluetooth, экран телефона, gps – IMRAN

Смежные вопросы