有谁知道丢失的权限如何表现以及何时在logcat中显示?
我试图故意删除INTERNET权限以触发此异常,但在下面的httpsURLConnection.connect()期间它根本没有被触发 - 它会直接进入finally块。
最初我认为这是因为之前授予了权限并且app / test设备记住了它,所以我卸载了应用程序然后重新安装它,但同样的事情发生了。
有谁知道触发此行为的原因是什么?谢谢!
编辑:我有另一个应用程序(来自Udacity课程的Sunshine应用程序),我从中复制了此代码,并显示了权限错误。
摘自我的班级 - 期望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;
}
}
}
答案 0 :(得分:0)
对于Android M及更高版本,请查看Android文档..
您不需要获得普通权限的权限访问权限 https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
https://developer.android.com/guide/topics/security/normal-permissions.html
答案 1 :(得分:0)
对于Marshmallow及以上版本,您需要在运行时请求权限。在AndroidManifest.xml中提及它们不是必需的,
请参阅以下链接, https://developer.android.com/training/permissions/requesting.html
答案 2 :(得分:-1)
至于marshmallow及以上android有一个新的运行时权限系统。你在运行时要求。将以下代码段添加到您的类中,这将帮助您请求运行时权限。 private 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();
}
}
}
然后你在doInBackground
方法
这样做
if(isInternetnAllowed()){
//do your doInbackground stuff
}else {
requestInternetPermission();
}
希望有所帮助