GPS切り替えはできない(?)

2011/04/04

Xperia arcの実機だが、GPSの切り替えがセキュリティポリシ上、アプリからできないみたいだ。

LocationManager を試してみる

[java] function void changeGps(boolean isGps) { LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); if (locationManager != null) { if (isGps) { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); Settings.System.putInt(getContentResolver(), Settings.System.LOCATION_PROVIDERS_ALLOWED, 1); //Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, 1); } else { locationManager.removeUpdates(this); Settings.System.putInt(getContentResolver(), Settings.System.LOCATION_PROVIDERS_ALLOWED, 0); //Settings.Secure.putInt(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED, 0); } } } [/java] ここでSettiings.Secure 部分を有効にしてマニュフェストファイルで android.permission.WRITE_SECURE_SETTINGS と設定してもエラーになる。

非公開APIを使う

ちなみにGoogle非公開のやり方では、機種によって動作した。 ※Xperia arcでは動かなかったが・・・ [java] function void changeGpsOn() { Uri uri = Uri.parse("custom:3"); Intent intent = new Intent().setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider") .addCategory(Intent.CATEGORY_ALTERNATIVE) .setData(uri); sendBroadcast(intent); } [/java] custom:3 ってのがGPSらしい。

参考

GPSの設定を変更する方法