android wifimanager权限,Android 6.0.1 - 权限问题= wifiManager.getScanResults()返回0
permissionsList.add()不起作用,但MainActivity.this.requestPermissions()正常工作。問題是它帶來一個對話框詢問用戶是否允許位置權限。Android 6.0.1 - 權限問題= wifiManager.getScanResults()返回0
為什么添加權限不起作用?
有沒有辦法避免對話框?
見我下面最少的代碼:
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 permissionsList = new ArrayList();
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 listeScan = wifiManager.getScanResults();
}
}
public WifiManager getCurrentWifiManager() {
return wifiManager;
}
}
這里是manifest.xml中:
2016-10-18
narb
總結
以上是生活随笔為你收集整理的android wifimanager权限,Android 6.0.1 - 权限问题= wifiManager.getScanResults()返回0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华硕u4000u 怎么重装 华硕u400
- 下一篇: 苹果笔记本自带win7系统怎么安装系统