Android 6.0.1-权限问题= wifiManager.getScanResults()返回0 [英] Android 6.0.1 - Permission issue = wifiManager.getScanResults() returns 0

查看:153
本文介绍了Android 6.0.1-权限问题= wifiManager.getScanResults()返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PermissionsList.add()不起作用,但是MainActivity.this.requestPermissions()可以正常工作.问题是它带来一个对话框,询问用户是否允许位置权限.

The permissionsList.add() doesn't work but MainActivity.this.requestPermissions() works fine. The issue is that it brings a dialog box asking if the user allow the location permission.

为什么添加权限无效?

有没有办法避免出现该对话框?

Is there a way to avoid the dialog box?

请参见下面的最小代码:

See my minimal code below:

public class MainActivity extends AppCompatActivity {
WifiManager wifiManager;
WifiBroadcastReceiver broadcastReceiver;

Context context;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    context = getApplicationContext();

    List<String> permissionsList = new ArrayList<String>();
    permissionsList.add(Manifest.permission.ACCESS_FINE_LOCATION);
    permissionsList.add(Manifest.permission.ACCESS_COARSE_LOCATION);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if(checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            MainActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
            MainActivity.this.requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 100);
            }
        }

        Button scan = (Button) findViewById(R.id.scan);

        wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(true);

        scan.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               if(wifiManager != null)
                    wifiManager.startScan();
            }
        });


        broadcastReceiver = new WifiBroadcastReceiver();

        // On attache le receiver au scan result
        registerReceiver(broadcastReceiver, new IntentFilter(
                WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));

    }

    public class WifiBroadcastReceiver extends BroadcastReceiver {

        private WifiManager wifiManager;


        @Override
        public void onReceive(Context context, Intent intent) {
            wifiManager = ((MainActivity) context).getCurrentWifiManager();
            List<ScanResult> listeScan = wifiManager.getScanResults();
          }
    }


    public WifiManager getCurrentWifiManager() {
        return wifiManager;
    }

 }

这是manifest.xml:

And here is the manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.bernard_zelmans.checksecurity">

    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Ping"></activity>
    </application>

</manifest>

推荐答案

从Android 6.0(API级别23)开始,用户向以下用户授予权限 应用在运行时(而不是在安装应用时)运行.这 这种方法简化了应用程序的安装过程,因为用户没有 他们在安装或更新应用程序时需要授予权限.它也是 使用户可以更好地控制应用程序的功能;例如, 用户可以选择授予相机应用访问该相机的权限,但不能 到设备位置.用户可以随时撤消权限 时间,请转到应用的设置"屏幕.

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen.

资源以及更多阅读内容:

Resource and more to read:

  • https://developer.android.com/training/permissions/requesting.html
  • https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous

这篇关于Android 6.0.1-权限问题= wifiManager.getScanResults()返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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