android – 在调用WifiManager startScan时抛出SecurityException

android – 在调用WifiManager startScan时抛出SecurityException,第1张

概述我正在使用AlarmManager发布的PendingIntent(使用setRepeating)每隔几分钟启动WiFi扫描(使用IntentService). 在大多数设备上,在大多数情况下,没有问题. 但是,在几个设备上,我收到以下错误(无法在任何测试设备上重现错误,这是用户设备的崩溃日志): java.lang.RuntimeException: Unable to start servic 我正在使用AlarmManager发布的PendingIntent(使用setRepeating)每隔几分钟启动WiFi扫描(使用IntentService).
在大多数设备上,在大多数情况下,没有问题.
但是,在几个设备上,我收到以下错误(无法在任何测试设备上重现错误,这是用户设备的崩溃日志):
java.lang.RuntimeException: Unable to start service com.myapp.androID.service.MyService@44a9701 with Intent { act=com.myapp.androID.ACTION_PERFORM_WIFI_SCAN flg=0x4 cmp=com.myapp/com.mayapp.androID.service.MyService (has extras) }: java.lang.SecurityException: Permission Denial: broadcast from androID asks to run as user -1 but is calling from user 0; this requires androID.permission.INteraCT_ACROSS_USERS_FulL or androID.permission.INteraCT_ACROSS_USERS       at androID.app.ActivityThread.handleServiceArgs(ActivityThread.java:3021)       at androID.app.ActivityThread.-wrap17(ActivityThread.java)       at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1443)       at androID.os.Handler.dispatchMessage(Handler.java:102)       at androID.os.Looper.loop(Looper.java:148)       at androID.app.ActivityThread.main(ActivityThread.java:5415)       at java.lang.reflect.Method.invoke(Method.java)       at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:725)       at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:615)Caused by: java.lang.SecurityException: Permission Denial: broadcast from androID asks to run as user -1 but is calling from user 0; this requires androID.permission.INteraCT_ACROSS_USERS_FulL or androID.permission.INteraCT_ACROSS_USERS       at androID.os.Parcel.readException(Parcel.java:1599)       at androID.os.Parcel.readException(Parcel.java:1552)       at androID.net.wifi.IWifiManager$Stub$Proxy.startScan(IWifiManager.java:1045)       at androID.net.wifi.WifiManager.startScan(WifiManager.java:1088)       ...

我正在从我的应用程序创建PendingIntent,所以我看不到从WifiManager抛出的SecurityException的原因(特别是因为这很少发生).

从PendingIntent代码启动的IntentService如下:

mContext.registerReceiver(mWifiScanReceiver,new IntentFilter(                    WifiManager.SCAN_RESulTS_AVAILABLE_ACTION));boolean ok = mWifiManager.startScan();

关于什么可能导致这个的任何想法?

解决方法 这是因为androID m的新应用权限.
看到上面的注释wifimanager的getScanResults()的源代码API 23-
/**     * Return the results of the latest access point scan.     * @return the List of access points found in the most recent scan. An app must hold     * {@link androID.Manifest.permission#ACCESS_COARSE_LOCATION ACCESS_COARSE_LOCATION} or     * {@link androID.Manifest.permission#ACCESS_FINE_LOCATION ACCESS_FINE_LOCATION} permission     * in order to get valID results.     */public List<ScanResult> getScanResults() {        try {            return mService.getScanResults(mContext.getopPackagename());        } catch (remoteexception e) {            return null;        }    }

因此,您必须向用户询问运行时的权限.将这些权限放在您的清单中 –

<uses-permission androID:name="androID.permission.ACCESS_FINE_LOCATION"/>  <uses-permission androID:name="androID.permission.INTERNET"/>  <uses-permission androID:name="androID.permission.ACCESS_WIFI_STATE"/>  <uses-permission androID:name="androID.permission.ACCESS_NETWORK_STATE"/>

从API 23起,您需要访问用户位置才能使用它.我建议您只有在授予权限的情况下才会使用基于API级别的权限检查和启动意图.
像这样的东西 –

if (Build.VERSION.SDK_INT >= 23) {int hasReadLocationPermission = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);      if (hasReadLocationPermission != PackageManager.PERMISSION_GRANTED) {        if (!ActivityCompat.shouldShowRequestPermissionRationale(HomeActivity.this,Manifest.permission.ACCESS_FINE_LOCATION)) {          showMessageOKCancel("You need to allow access to GPS",new DialogInterface.OnClickListener() {                @OverrIDe                public voID onClick(DialogInterface dialog,int which) {                  ActivityCompat.requestPermissions(HomeActivity.this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},GPS_ENABLE_REQUEST);                }              });          return;        }        ActivityCompat.requestPermissions(HomeActivity.this,GPS_ENABLE_REQUEST);        return;      }      if (locationManager != null && !locationManager.isProvIDerEnabled(LocationManager.GPS_PROVIDER)) {        gotoGPSEnableScreen();      } else {        //Permissions granted and gps is on        launchService(true);      }}

进一步检查结果 –

@OverrIDe  public voID onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) {    switch (requestCode) {      case GPS_ENABLE_REQUEST:        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {          if (!locationManager.isProvIDerEnabled(LocationManager.GPS_PROVIDER)) {            gotoGPSEnableScreen();          }        } else {          launchService(false);        }      default:        return;    }  }

更新:

androID.permission.INteraCT_ACROSS_USERS_FulL是一个签名级别的权限.
只需在你的清单中添加这个androID:protectionLevel =“signature”.

有关详细信息,您可以查看这一点

http://developer.android.com/guide/topics/manifest/permission-element.html

<permission androID:name="androID.permission.INteraCT_ACROSS_USERS_FulL" androID:protectionLevel="signature"/>
总结

以上是内存溢出为你收集整理的android – 在调用WifiManager startScan时抛出SecurityException全部内容,希望文章能够帮你解决android – 在调用WifiManager startScan时抛出SecurityException所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1131639.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存