gpt4 book ai didi

Android 6.0.1 - 权限问题 = wifiManager.getScanResults() 返回 0

转载 作者:行者123 更新时间:2023-11-29 01:15:22 29 4
gpt4 key购买 nike

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

为什么添加权限不起作用?

有没有办法避免这个对话框?

请参阅下面我的最小代码:

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:

<?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>

最佳答案

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.

资源和更多阅读:

关于Android 6.0.1 - 权限问题 = wifiManager.getScanResults() 返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40116612/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com