[英]Permission denied (missing INTERNET permission?) Not Appearing in logcat
Does anyone know how the missing permissions behave and when is it shown in logcat? 有谁知道丢失的权限的行为以及何时在logcat中显示?
I tried removing the INTERNET permission intentionally to trigger this exception, but it's not being triggered at all during httpsURLConnection.connect() below -- what happens is that it goes straight to the finally block. 我试着故意删除INTERNET权限以触发此异常,但是在下面的httpsURLConnection.connect()期间根本没有触发它-发生的事情是它直接进入了finally块。
Initially I thought it's because the permission was granted before and the app/test device remembers it, so I uninstalled the app then reinstalled it but the same thing happens. 最初,我认为这是因为之前已经授予了权限,并且应用程序/测试设备会记住它,所以我卸载了该应用程序,然后重新安装了它,但是发生了同样的事情。
Does anyone know what triggered this behavior? 有谁知道触发此行为的原因? Thanks! 谢谢!
Edit: I have another app (Sunshine app from the Udacity course) where I copied this code from, and that one shows the permission error. 编辑:我有另一个应用程序(来自Udacity课程的Sunshine应用程序),我从中复制了此代码,该应用程序显示了权限错误。
Excerpt from my class -- expecting a Permission denied (missing INTERNET permission?) in the httpsURLConnection.connect() line 我的课程节选-期望httpsURLConnection.connect()行中的权限被拒绝(缺少INTERNET权限?)
public class MovieDBAPI extends AsyncTask<String, Object, List<Movie>> {
final String TAG = getClass().getSimpleName();
protected List<Movie> doInBackground(String... params) {
BufferedReader bufferedReader = null;
HttpsURLConnection httpsURLConnection = null;
StringBuffer stringBuffer = null;
try {
//create a URL
URL url = new URL(buildURL(params[0]));
Log.v(TAG, url.toString());
httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.connect();
//get string input
InputStream inputStream = httpsURLConnection.getInputStream();
if (inputStream == null) {
//no input stream, nothing to do
return null;
}
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
stringBuffer = new StringBuffer();
while((line = bufferedReader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
stringBuffer.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//if stringBuffer is not null, then prepare the result
if (stringBuffer != null) {
return getMovieDataFromJSON(stringBuffer.toString());
}
if (httpsURLConnection != null) {
httpsURLConnection.disconnect();
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
}
}
For Android M and above, check Android docs.. 对于Android M及更高版本,请检查Android文档。
You don't need to permission access for normal permissions https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous 您不需要普通权限的权限访问https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
https://developer.android.com/guide/topics/security/normal-permissions.html https://developer.android.com/guide/topics/security/normal-permissions.html
For Marshmallow and above, you would need to request permissions at runtime. 对于棉花糖及更高版本,您需要在运行时请求权限。 Mentioning them in AndroidManifest.xml isn't necessary, 无需在AndroidManifest.xml中提及它们,
Please refer to the following link, https://developer.android.com/training/permissions/requesting.html 请参考以下链接, https://developer.android.com/training/permissions/requesting.html
As for marshmallow and above android has a new run time permission system. 至于棉花糖及以上,android有一个新的运行时权限系统。 You have ask for at run time . 您要求在运行时。 Add the following snippet into your class which will help you to ask for run time permission . 将以下代码段添加到您的课程中,这将帮助您请求运行时权限。 private int INTERNET_PERMISSION_CODE = 23; 私有int INTERNET_PERMISSION_CODE = 23;
//We are calling this method to check the permission status
private boolean isInternetnAllowed() {
//Getting the permission status
int result = ContextCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
//If permission is granted returning true
if (result == PackageManager.PERMISSION_GRANTED)
return true;
//If permission is not granted returning false
return false;
}
//Requesting permission
private void requestInternetPermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.INTERNET)){
//If the user has denied the permission previously your code will come to this block
//Here you can explain why you need this permission
//Explain here why you need this permission
}
//And finally ask for the permission
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.INTERNET}, INTERNET_PERMISSION_CODE);
}
//This method will be called when the user will tap on allow or deny
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//Checking the request code of our request
if(requestCode == INTERNET_PERMISSION_CODE){
//If permission is granted
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//Displaying a toast
Toast.makeText(this,"Permission granted for internet",Toast.LENGTH_LONG).show();
}else{
//Displaying another toast if permission is not granted
Toast.makeText(this,"Oops you just denied the permission",Toast.LENGTH_LONG).show();
}
}
}
Then what ever you are doing in doInBackground
method 然后,您在doInBackground
方法中所做的doInBackground
do it like this way 这样做
if(isInternetnAllowed()){
//do your doInbackground stuff
}else {
requestInternetPermission();
}
Hope it helps 希望能帮助到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.