需要动态申请的权限:
需求:现在要调用照相机照照片做头像-_-
以下代码是根据鸿洋大神的Demo沾的, 做以后开发时忘了可以回头看看
public class MainActivity extends AppCompatActivity {
private Button id_btn_callphone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
id_btn_callphone = (Button) findViewById(R.id.id_btn_callphone);
id_btn_callphone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CALL_PHONE},
1);
} else {
callPhone();
}
}
});
}
/**
* 打电话
*/
private void callPhone() {
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" + "10086");
intent.setData(data);
startActivity(intent);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if (requestCode == 1)
{
if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
callPhone();
} else
{
// Permission Denied
Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
下面几部记录了我动态申请权限的步骤
1, 拨打电话时检查权限
2, 有就拨打电话; 若是没有的话就申请权限
3, 权限申请的回调在onRequestPermissionsResult 这个方法中执行, 这时若是用户同意, 那就有权限, 拨打电话, 用户不同意, 只能弹toast提示没权限拨打不了...
我拿oppoA57手机测试(6.0.1系统), 一切正常..