2016-05-06 3 views

答えて

0

はあなたがここにLocationSensorチュートリアルを読むことをお勧めしますLocationSensor.Enabled = False

により、GPSをオフにすることができ、コード

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); 
    } 
} 

の下に使用してみてください: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

関連する問題