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

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐