【问题标题】:Android check GPS availability on device [duplicate]Android检查设备上的GPS可用性[重复]
【发布时间】:2011-11-03 04:28:30
【问题描述】:

可能重复:
Programmatically find device support GPS or not?

我如何检查设备上的 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");
}

我总是有 GPS,但我的爱可视 101IT 没有 GPS 模块。有没有办法检查硬件 GPS 的可用性?

【问题讨论】:

    标签: android gps archos


    【解决方案1】:

    使用此代码

    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

    【讨论】:

    • 我有一个没有 GPS 的设备,但是 pm.hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);总是返回 True
    • LocationManager.isProviderEnabled(String provider) 为我的平板电脑返回 false,这是真的。但是,如果我在支持 GPS 的手持设备上禁用了 GPS,它也会是错误的。我需要检查硬件可用性,而不是启用-禁用状态
    • 您是否从您的设备中获取了AllProviders 并检查是否存在GPS 提供程序。
    • 在我的其他设备上启用 GPS 时,会显示 GPS 提供程序,但当我禁用它时,只有网络。所以,我不能用那个方法
    • 到目前为止,我没有找到任何方法,因为类和包对于 android OS Ex:android 2.2 是常见的,不适用于任何特定设备。
    【解决方案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);
    

    【讨论】:

    • 从您的代码 sn-p 关闭和打开我的 gps 时如何总是正确的?您确定这会检测 GPS 是否处于活动状态,还是会检查 GPS 是否可用或允许或其他?
    猜你喜欢
    • 2016-02-03
    • 2016-07-07
    • 1970-01-01
    • 2021-01-28
    • 2016-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    相关资源
    最近更新 更多