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