SCAN_RESULTS_AVAILABLE_ACTION返回的Andr​​oid 6.0空单 [英] SCAN_RESULTS_AVAILABLE_ACTION return empty list in Android 6.0

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

问题描述

昨天,我的Nexus 5收到 Android的MNC 的更新版本 6.0 - 棉花糖。 从那时起,以扫描在装置停止提供所述网络接收所述列表,在这种情况下,结果列表中的动作具有0的大小,即使使用10+无线网络中的无线上网的系统设置中列出

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.

在code因为这是通常的:注册 SCAN_RESULTS_AVAILABLE_ACTION 并等待在接收器的情况下,像这样的:

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?

推荐答案

由于安卓6.0,许可行为已更改运行时。使用此功能,需要一个许可,应首先检查权限授予previously。使用 checkSelfPermission(permissionString) <一个href="https://developer.android.com/reference/android/content/Context.html#checkSelfPermission(java.lang.String)"相对=nofollow> 结果返回的方法,枯萎疗法权限 PERMISSION_GRANTED PERMISSION_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
}

如果您的$ C $,C是设备之前m跑,你继续你的code,允许使用传统的方法理所当然的。

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();
     }
 }

在这之后,你可以检查位置服务为ON时,使用 LocationServices.SettingsApi ,并要求用户启用如果该选项被禁用。这是可能的播放服务 LocationSettingsStatus codes.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返回的Andr​​oid 6.0空单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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