カテゴリー
SugiBlog Webエンジニアのためのお役立ちTips

GPS機能設定の状態を取得

この記事は最終更新日から1年以上経過しています。

端末自身の[設定]-[位置情報とセキュリティ]より、GPS機能がONに設定されているかを取得します。
OFFに設定されている場合にダイアログを表示して設定画面を呼び出す処理をしています。

1// GPS機能設定の状態を取得
2String GpsStatus = android.provider.Settings.Secure.getString(getContentResolver(), Secure.LOCATION_PROVIDERS_ALLOWED);
3 
4if (GpsStatus.indexOf("gps", 0) < 0) {
5  //GPSが無効だった場合
6  AlertDialog.Builder ad = new AlertDialog.Builder(this);
7  ad.setMessage("GPS機能がOFFになっています。\n設定画面を開きますか?");
8  ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
9    public void onClick(DialogInterface dialog, int whichButton) {
10      //設定画面を呼び出す
11      Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
12      startActivity(intent);
13    }
14  });
15  ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
16    public void onClick(DialogInterface dialog,int whichButton) {
17      //何もしない
18      }
19  });
20  ad.create();
21  ad.show();
22} else {
23  //GPSが有効だった場合
24}

API Level3(Android 1.5)以降、セキュリティ上の問題で設定を強制的に変更することはできません。

この記事がお役に立ちましたらシェアお願いします
4,336 views

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です