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>
答案 0 :(得分:1)
从Android 6.0(API级别23)开始,用户授予权限 应用程序运行时的应用程序,而非安装应用程序时的应用程序。这个 方法简化了应用安装过程,因为用户没有 需要在安装或更新应用程序时授予权限。它也是 让用户更好地控制应用程序的功能;例如, 用户可以选择让相机应用程序访问相机,但不能 到设备位置。用户可以撤消任何权限 时间,转到应用程序的“设置”屏幕。
资源和更多内容: