Android检查设备上的GPS可用性

ska*_*red 12 gps android archos

可能重复:以
编程方式查找设备是否支持GPS?

我该如何检查,设备上有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");
}
Run Code Online (Sandbox Code Playgroud)

我总是得到HAS GPS,但我的Archos 101IT没有GPS模块.有没有办法检查硬件GPS的可用性?

Kar*_*thi 28

使用此代码

boolean hasGps = pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
Run Code Online (Sandbox Code Playgroud)

如果您的设备嵌入了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();
}
Run Code Online (Sandbox Code Playgroud)

您可以调用LocationManager.getAllProviders()并检查LocationManager.GPS_PROVIDER列表中是否包含.

否则你只需使用这种代码 LocationManager.isProviderEnabled(String provider)方法.

如果您的设备中没有GPS,即使返回false也是如此

  • LocationManager.isProviderEnabled(String provider)为我的平板电脑返回false,这是真的.但是,如果我在支持GPS的手持设备上禁用了GPS,那么它也将是假的.我需要检查硬件可用性,而不是启用禁用状态 (3认同)
  • 我有一个没有GPS的devide,但是pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS); 总是返回True (2认同)