使用此代码
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)
方法。
如果连的情况下返回false,如果客人不愿意在你的设备有GPS
我有一个没有GPS的设备,但pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);总是返回True – skayred
请参阅编辑并尝试 – Karthi
LocationManager.isProviderEnabled(String provider)为我的平板电脑返回false,这是真的。但是,如果我的掌上电脑支持GPS,我的GPS已禁用,这也将是错误的。我需要检查硬件可用性,未启用禁用状态 – skayred