210

Android Cupcake (1.5) 対応デバイスで、GPS を確認して有効にするにはどうすればよいですか?

4

11 に答える 11

131

Androidでは、LocationManagerを使用して、デバイスでGPSが有効になっているかどうかを簡単に確認できます。

これがチェックする簡単なプログラムです。

GPSが有効かどうか:-AndroidManifest.xmlの以下のユーザー許可行をアクセス場所に追加します

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Javaクラスファイルは次のようになります

public class ExampleApp extends Activity {
    /** Called when the activity is first created. */
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
            Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
        }else{
            showGPSDisabledAlertToUser();
        }
    }

    private void showGPSDisabledAlertToUser(){
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
        .setCancelable(false)
        .setPositiveButton("Goto Settings Page To Enable GPS",
                new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                Intent callGPSSettingIntent = new Intent(
                        android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(callGPSSettingIntent);
            }
        });
        alertDialogBuilder.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int id){
                dialog.cancel();
            }
        });
        AlertDialog alert = alertDialogBuilder.create();
        alert.show();
    }
}

出力は次のようになります

ここに画像の説明を入力してください

ここに画像の説明を入力してください

于 2011-11-03T06:14:41.027 に答える
39

はいGPS設定はプライバシー設定であるため、プログラムで変更することはできません。プログラムからオンになっているかどうかを確認し、オンになっていない場合は処理する必要があります。GPSがオフになっていることをユーザーに通知し、必要に応じてこのようなものを使用して設定画面をユーザーに表示できます。

ロケーションプロバイダーが利用可能かどうかを確認します

    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
    if(provider != null){
        Log.v(TAG, " Location providers: "+provider);
        //Start searching for location and update the location text when update available
        startFetchingLocation();
    }else{
        // Notify users and show settings if they want to enable GPS
    }

ユーザーがGPSを有効にしたい場合は、この方法で設定画面を表示できます。

Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(intent, REQUEST_CODE);

また、onActivityResultで、ユーザーがそれを有効にしているかどうかを確認できます

    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == REQUEST_CODE && resultCode == 0){
            String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
            if(provider != null){
                Log.v(TAG, " Location providers: "+provider);
                //Start searching for location and update the location text when update available. 
// Do whatever you want
                startFetchingLocation();
            }else{
                //Users did not switch on the GPS
            }
        }
    }

それはそれを行う一つの方法であり、私はそれが役立つことを願っています。何か間違ったことをしている場合はお知らせください。

于 2010-02-05T00:01:21.170 に答える
32

手順は次のとおりです。

ステップ 1:バックグラウンドで実行されるサービスを作成します。

ステップ 2:マニフェスト ファイルでも次の権限が必要です。

android.permission.ACCESS_FINE_LOCATION

ステップ 3:コードを書く:

 final LocationManager manager = (LocationManager)context.getSystemService    (Context.LOCATION_SERVICE );

if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) )
  Toast.makeText(context, "GPS is disabled!", Toast.LENGTH_LONG).show(); 
else
  Toast.makeText(context, "GPS is enabled!", Toast.LENGTH_LONG).show();

ステップ 4:または、以下を使用して簡単に確認できます。

LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
boolean statusOfGPS = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);

ステップ 5:サービスを継続的に実行して、接続を監視します。

于 2014-03-05T09:37:54.553 に答える
18

Yes you can check below is the code:

public boolean isGPSEnabled (Context mContext){
    LocationManager locationManager = (LocationManager)
                mContext.getSystemService(Context.LOCATION_SERVICE);
    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
}
于 2015-03-19T08:25:51.997 に答える
8

これが私の場合に機能したスニペットです

final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
    buildAlertMessageNoGps();
}

`

于 2016-09-21T07:28:05.210 に答える
6

ユーザーが設定で GPS の使用を許可している場合、GPS が使用されます。

これを明示的にオンにすることはできなくなりましたが、その必要はありません。これは実際にはプライバシー設定であるため、微調整する必要はありません。ユーザーがアプリが正確な座標を取得することに問題がなければ、それはオンになります。次に、ロケーション マネージャー API は、可能であれば GPS を使用します。

アプリが GPS なしでは役に立たず、オフになっている場合は、インテントを使用して右側の画面で設定アプリを開いて、ユーザーが有効にできるようにすることができます。

于 2009-05-10T12:40:58.333 に答える