我是Android工作室的新手,我之前在我的应用程序上工作Android版本5.1它工作正常但现在在6我无法获得许可或我不确定我的方法是否错误。我正在创建一个应用程序,它使用我的手机可用的访问点列表。但即使我被授予权限,我的应用程序也没有向我显示wifi列表,但相同的代码适用于早期版本。
Mainactivity:
package com.wiferange.wifi_test;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView lv;
String lists[]={"a","b"};
WifiManager wifi;
String wifis[];
WifiScanReceiver wifiReciever;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lv = (ListView)findViewById(R.id.listView);
wifi=(WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiReciever = new WifiScanReceiver();
getPermission();
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
protected void onPause() {
unregisterReceiver(wifiReciever);
super.onPause();
}
protected void onResume() {
registerReceiver(wifiReciever, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
super.onResume();
}
private void getPermission() {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.ACCESS_WIFI_STATE) != PackageManager.PERMISSION_GRANTED){
requestPermissions(new String[]{Manifest.permission.ACCESS_WIFI_STATE},0x12345);
// lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,lists));
//After this point you wait for callback in onRequestPermissionsResult(int, String[], int[]) overriden method
}else{
wifi.startScan();
//do scanning, permission was previously granted; or legacy device
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { //funcitno executes when some permission was granted
if (requestCode == 0x12345) {
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) { //check if permission was already grnated and start scannig if yes
wifi .startScan() ;
return;
}
}
getPermission(); //ask for permission if not given
}
}
private class WifiScanReceiver extends BroadcastReceiver {
public void onReceive(Context c, Intent intent) {
List<ScanResult> wifiScanList = wifi.getScanResults();
wifis = new String[wifiScanList.size()];
for(int i = 0; i < wifiScanList.size(); i++){
wifis[i] = ((wifiScanList.get(i)).toString());
}
lv.setAdapter(new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,wifis));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
表现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wiferange.wifi_test">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
请帮我解决这个问题。提前致谢 !!!!
您正在请求错误的运行时权限。根据Android 6.0 changes:
要通过蓝牙和Wi-Fi扫描访问附近外部设备的硬件标识符,您的应用现在必须具有ACCESS_FINE_LOCATION或ACCESS_COARSE_LOCATION权限
您必须请求并获得其中一个运行时权限才能执行wifi扫描。
如果权限位于自动授予权限列表或“危险”权限列表中,则需要查看权限。如果那是在危险许可列表中,那么你需要像android M这样的运行时权限。只是去看看这个link你正在请求错误的运行时权限。 ACCESS_WIFI_STATE权限自动被授予权限,无需访问它运行时。
看来为了在从Android 6.0扫描Connections时,WifiManager能够很好地访问你的位置,所以这是好位置或粗略位置,我将以下内容添加到我的Manifest文件中:
我想这就是你需要的。 Application (WiFi connections) doesn't work anymore on Android 6.0 Marshmallow