android - 即使在某些设备上授予所有必需的权限后,调用 WifiManager.startLocalOnlyHotspot() 也会引发 SecurityException

标签 android android-permissions wifimanager securityexception runtime-permissions

我正在开发一个文件共享应用程序。我需要通过调用 WifiManager.startLocalOnlyHotspot() 以编程方式打开设备的仅限本地热点。

根据此页面上的 android 文档 - https://developer.android.com/reference/android/net/wifi/WifiManager#startLocalOnlyHotspot(android.net.wifi.WifiManager.LocalOnlyHotspotCallback,%2520android.os.Handler) ,

Applications need to have the following permissions to start LocalOnlyHotspot: Manifest.permission.CHANGE_WIFI_STATE and ACCESS_FINE_LOCATION. Callers without the permissions will trigger a SecurityException.

我已在 list 中添加了这两个权限,并在运行时授予了 ACCESS_FINE_LOCATION (因为这是运行时权限(危险权限))。

但在某些设备中调用 startLocalOnlyHotspot() 仍然会抛出 SecurityException。
引发 SecurityException 的设备:Samsung Galaxy J7 Max (j7maxlte)、Android 8.1 运行正常且未抛出异常的设备:Redmi Note 7 Pro、Android 9 PKQ1.181203.001

我错过了什么?

最佳答案

您需要启用 GPS 定位。

首先添加gms的依赖。

implementation 'com.google.android.gms:play-services-location:16.0.0'

之后,设置位置服务

  private void setupLocationServices() {
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10);
    mLocationRequest.setSmallestDisplacement(10);
    mLocationRequest.setFastestInterval(10);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    LocationSettingsRequest.Builder builder = new
        LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);

    task = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());

    locationSettingsResponseBuilder();
  }

LocationSettingsResponseBuilder:

  private void locationSettingsResponseBuilder() {
    task.addOnCompleteListener(task -> {
      try {
        LocationSettingsResponse response = task.getResult(ApiException.class);
        // All location settings are satisfied. The client can initialize location
        // requests here.

        //Everything is good. You can turn on hotspot here.

        //}
      } catch (ApiException exception) {
        switch (exception.getStatusCode()) {
          case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
            // Location settings are not satisfied. But could be fixed by showing the
            // user a dialog.
            try {
              // Cast to a resolvable exception.
              ResolvableApiException resolvable = (ResolvableApiException) exception;
              // Show the dialog by calling startResolutionForResult(),
              // and check the result in onActivityResult().
              resolvable.startResolutionForResult(
                  MainActivity.this,
                  101);
            } catch (IntentSender.SendIntentException e) {
              // Ignore the error.
            } catch (ClassCastException e) {
              // Ignore, should be an impossible error.
            }
            break;
          case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
            // Location settings are not satisfied. However, we have no way to fix the
            // settings so we won't show the dialog.
            break;
        }
      }
    });
  }

将此案例添加到 onActivityResult 方法中。

      case 101:
        final LocationSettingsStates states = LocationSettingsStates.fromIntent(data);
        switch (resultCode) {
          case Activity.RESULT_OK:
            // All required changes were successfully made
            Log.v("case 101", states.isLocationPresent() + "");

            //Everything is good. You can turn on hotspot here.

            break;
          case Activity.RESULT_CANCELED:
            // The user was asked to change settings, but chose not to
            Log.v("case 101", "Canceled");
            break;
          default:
            break;
        }
        break;

我最近创建了一个名为 Spotserve 的演示应用程序。这将为所有 API>=15 的设备打开 WiFi 热点,并在该热点上托管一个演示服务器。您可以检查以获取更多详细信息。希望这有帮助!

关于android - 即使在某些设备上授予所有必需的权限后,调用 WifiManager.startLocalOnlyHotspot() 也会引发 SecurityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56082255/

相关文章:

android - 在 v23 上找不到 Symbol Manifest.permission.WRITE_EXTERNAL_STORAGE

android - 调用 WifiManager startScan 时抛出 SecurityException

android - 如何使用 SUPPLICANT_STATE_CHANGED_ACTION WiFi BroadcastReceiver - android

android - 无法对非静态方法 findviewbyid(int) TextView 进行静态引用

android - initWithAppID 命令崩溃

Android:捕获图像 Intent ,照片文件大小为零字节?

java - 如何让Gson的fromJson创建对象的arraylilst

android - 由于 RECEIVE_SMS 权限被拒绝,Cordova Android 应用程序在 Android P 上崩溃

android - getAccountsByType ("com.google") 无法为 minSdkVersion 21 和 targetSdkVersion 27 列出我的 Google 帐户

android - 在android中设置wifi配置的优先级