美文网首页
动态权限申请

动态权限申请

作者: To_Be_Better | 来源:发表于2018-04-24 15:10 被阅读0次

    Android6.0之后安全权限需要使用动态申请

    检查是否有授权

    ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != 
    PackageManager.PERMISSION_GRANTED
    

    第一个参数:检查权限的context
    第二个参数:检查的权限

    申请权限

    ActivityCompat.requestPermissions(MainActivity.this, 
    new String[]{Manifest.permission.CALL_PHONE}, 
    CALL_PHONE_CODE);
    

    第一个参数:目标activity
    第二个参数:申请的权限
    第三个参数:回掉时的code

    重写回掉函数onRequestPermissionsResult

    package com.example.runtimepermission;
    
    import android.Manifest;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import android.support.annotation.NonNull;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends AppCompatActivity {
    
        private static int CALL_PHONE_CODE = 1;
        private static String TAG = "CALLPHONE";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Button btn_call = (Button) findViewById(R.id.btn_call);
            btn_call.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //先检查权限
                    if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                        Log.i(TAG, "PackageManager.PERMISSION_GRANTED: " + PackageManager.PERMISSION_GRANTED);
                        //
                        ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE_CODE);
                    } else {
                        call();
                    }
                }
            });
        }
    
        private void call() {
            try {
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:10086"));
                startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            Log.i(TAG, "requestCode: " + requestCode + "\npermissions: " + permissions.toString() + "\ngrantResults: " + grantResults.toString());
            switch (requestCode) {
                case 1:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        call();
                    } else {
                        Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
                    }
                    break;
                default:
            }
        }
    }
    
    

    相关文章

      网友评论

          本文标题:动态权限申请

          本文链接:https://www.haomeiwen.com/subject/qktwkftx.html