SCAN_RESULTS_AVAILABLE_ACTION 在 Android 6.0 中返回空列表 [英] SCAN_RESULTS_AVAILABLE_ACTION return empty list in Android 6.0

查看:27
本文介绍了SCAN_RESULTS_AVAILABLE_ACTION 在 Android 6.0 中返回空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我的 Nexus 5 收到了从 Android MNC6.0 - Marshmallow 版本的更新.从那以后,扫描设备中可用网络的操作停止接收列表,在这种情况下,结果列表的大小为 0,即使在 Wifi 系统设置中列出了 10 多个 Wifi 网络.

Yesterday my Nexus 5 receive the update from Android MNC to version 6.0 - Marshmallow. Since then, the action to scan the networks available in the device stop receiving the list, in this case the result list have a size of 0, even with 10+ Wifi networks listed in the Wifi system settings.

通常的代码是:注册 SCAN_RESULTS_AVAILABLE_ACTION 并在 Receiver 中等待事件,如下所示:

The code for this is the usual: Register the SCAN_RESULTS_AVAILABLE_ACTION and wait for the event in the Receiver, like this:

// Register the Receiver in some part os fragment...
getActivity().registerReceiver(wifiListener, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager.startScan();

// Inside the receiver:
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> results = wifiManager.getScanResults();
// the result.size() is 0 after update to Android v6.0, same code working in older devices.

我在 API 的更改 主题中搜索了有关此内容的内容,但我没有看到此功能的任何重大更改.

I searched in the changes of the API topic about this, but I didn' see any breaking changes for this functionality.

有人注意到了吗?API 中有什么新东西还是只是一个孤立的案例?

Did anyone notice this? Is something new in the API or just a isolated case?

推荐答案

从 Android 6.0 开始,权限行为已更改为运行时.要使用需要权限的功能,首先应该检查之前是否授予了权限.使用 checkSelfPermission(permissionString) method 返回结果,其权限为 PERMISSION_GRANTEDPERMISSION_DENIED.

As of Android 6.0, permission behaviour has changed to runtime. To use a feature that requires a permission, one should check first if the permission is granted previously. Using checkSelfPermission(permissionString) method a result is returned, wither ther permission is PERMISSION_GRANTED or PERMISSION_DENIED.

如果未授予许可或第一次获得许可,则应提出许可请求.为用户提供授予或拒绝的选项.

If permission isn't granted or it is first time, a request for permission should be made. Giving a user an option to grant or deny.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
   requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                 PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION);
    //After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method

}else{
    getScanningResults();
   //do something, permission was previously granted; or legacy device
}

如果您的代码在 M 之前的设备上运行,则继续您的代码,使用旧方法授予权限.

If your code is running on device prior to M, you proceed with your code, permission was granted using legacy method.

请求权限后,将向用户显示对话框.他/她的回复将作为:

Once requested for permission, dialog will be shown to user. His/her response will be delivered as:

@Override
 public void onRequestPermissionsResult(int requestCode, String[] permissions,
         int[] grantResults) {
     if (requestCode == PERMISSIONS_REQUEST_CODE_ACCESS_COARSE_LOCATION
             && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
         // Do something with granted permission
        mWifiListener.getScanningResults();
     }
 }

之后,您可以使用 LocationServices.SettingsApi 检查定位服务是否开启,如果此选项被禁用,则请求用户启用.这可以通过 Play 服务 LocationSettingsStatusCodes.RESOLUTION_REQUIRED 回调实现.

After that, you can check if the Location Services is ON, using LocationServices.SettingsApi and request the user to enable if this options is disabled. This is possible with Play Services LocationSettingsStatusCodes.RESOLUTION_REQUIRED callback.

这篇关于SCAN_RESULTS_AVAILABLE_ACTION 在 Android 6.0 中返回空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆