使用此代码
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