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.
Why adding the permission does not work?
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;
}
}
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>