我面临一个奇怪的问题.我遵循本教程,以便在运行时为我构建的应用程序请求权限.问题在于,即使我检查设备是否使用Android M并且我在运行时请求权限,它也不显示对话框.
代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_configuration); spinner = (Spinner)findViewById(R.id.database_selection_spinner); button = (Button)findViewById(R.id.proceedBtn); if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(ConfigurationActivity.this, Manifest.permission.INTERNET) == PackageManager.PERMISSION_GRANTED){ accessWebService(); }else { if (shouldShowRequestPermissionRationale(Manifest.permission.INTERNET)){ StringGenerator.showToast(ConfigurationActivity.this, "Internet Permission needs to be granted"); } requestPermissions(new String[]{Manifest.permission.INTERNET}, AppConstants.PERMISSION_REQ_CODE); } }else { accessWebService(); } } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == AppConstants.PERMISSION_REQ_CODE){ if (grantResults[0] == PackageManager.PERMISSION_GRANTED){ accessWebService(); }else { StringGenerator.showToast(ConfigurationActivity.this, "LOL"); } }else { super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }
accessWebService:
private void accessWebService(){ FetchDataTask task = new FetchDataTask(); task.execute(AppConstants.DATABASE_INFO_URL); }
清单:
结果如下:
并且没有要求许可:
有什么建议?
Internet是"正常权限",它由系统授予而无需询问用户.请参阅文档中的更多内容.
将仅询问用户危险权限.