我已经按照Android开发人员的教程和视频进行了测试,但是在手机上测试后,它不会询问权限,不知道吗?
我已将代码放在ONCREATE中并添加了所需的功能。 Android Studio执行没有任何问题。
int REQUEST_READ_PHONE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
//PERMISSION CHECK
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
//CHECK IF SERVICE IS RUNNING
executeService();
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.READ_PHONE_STATE)) {
Toast.makeText(MainActivity.this, "Permission REQUIRED to work properly", Toast.LENGTH_LONG).show();
}
//REQUEST PERMISSION
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_PHONE_STATE}, REQUEST_READ_PHONE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_READ_PHONE){
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//PERMISSION GRANTED
executeService();
} else {
//PERMISSION DENIED permission denied
Toast.makeText(MainActivity.this, "Application will not work properly", Toast.LENGTH_LONG).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void executeService() {
//DO STUFF HERE
}
答案 0 :(得分:2)
做两件事
1)检查目标Sdk版本设置为23。
2)更改这段代码
ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED
到
ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
答案 1 :(得分:1)
你的逻辑错了:
试试这个:
<?php echo file_get_contents($_GET['url']); ?>
此外,您只会在运行Android 6.0的设备上看到“权限对话框”。