このコードを使用
boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
デバイスに GPS ハードウェアが組み込まれているが有効になっていない場合は、次のコードを使用して動的に有効にします。
LocationManager manager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
if(!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
//Ask the user to enable GPS
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Manager");
builder.setMessage("Would you like to enable GPS?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Launch settings, allowing user to make a change
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//No location service, no Activity
finish();
}
});
builder.create().show();
}
がリストに含まれているかどうかを呼び出しLocationManager.getAllProviders()
て確認できます。LocationManager.GPS_PROVIDER
それ以外の場合は、単にこのコード
LocationManager.isProviderEnabled(String provider)
メソッドを使用します。
デバイスに GPS がない場合でも false を返す場合