如何检查设备上是否有GPS?
当我尝试使用下一个代码时
PackageManager pm = context.getPackageManager();
boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION);
if (hasGps) {
Log.d("TAG", "HAS GPS");
} else {
Log.d("TAG", "HAS NOT GPS");
}
我总是得到HAS GPS,但我的Archos 101IT没有GPS模块。有没有办法检查硬件GPS的可用性?
答案 0 :(得分:27)
使用此代码
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
答案 1 :(得分:2)
可以像调用任何其他活动一样调用android系统GPS设置屏幕:
startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), 0);
要检查GPS可用性,请使用以下代码:
(Activity必须实现LocationListener)
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,1.0f, this);
boolean isGPS = locationManager.isProviderEnabled (LocationManager.GPS_PROVIDER);