1. 程式人生 > >android傳送簡訊填入手機號碼,6.0動態請求許可權撥打電話

android傳送簡訊填入手機號碼,6.0動態請求許可權撥打電話

                //發簡訊填入號碼
                Uri uri = Uri.parse("smsto:" + phone);
                Intent sendIntent = new Intent(Intent.ACTION_VIEW, uri);
                sendIntent.putExtra("sms_body", subTitle);
                context.startActivityForResult(sendIntent, 1002 );
        //跳轉到撥打電話介面
        Uri uri = Uri.parse("tel:13876543210");
        Intent intent = new Intent(Intent.ACTION_DIAL,uri);
        startActivity(intent);


        //直接撥號
        Uri uri = Uri.parse("tel:13823214321");
        Intent intent = new Intent(Intent.ACTION_CALL, uri);
        startActivity(intent);

但是在6.0上面打電話需要動態請求許可權,所以程式碼又改成這樣:
    private void testCallPhone() {

        if (Build.VERSION.SDK_INT >= 23) {

            //判斷有沒有撥打電話許可權
            if (PermissionChecker.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                //請求撥打電話許可權
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CODE);

            } else {
                callPhone("13823214321");
            }

        } else {
            callPhone("13823214321");
        }
    }


    /**
     * 請求許可權的回撥方法
     * @param requestCode    請求碼
     * @param permissions    請求的許可權
     * @param grantResults   許可權的結果
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_CODE && PermissionChecker.checkSelfPermission(context, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
            ToastUtils.show(context, "授權成功");
            callPhone("13823214321");
        }
    }

    private void callPhone(String phoneNum) {
        //直接撥號
        Uri uri = Uri.parse("tel:" + phoneNum);
        Intent intent = new Intent(Intent.ACTION_CALL, uri);
        //此處不判斷就會報錯
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {

            startActivity(intent);
        }
    }

    private final int REQUEST_CODE = 0x1001;

需要動態申請的許可權還有
Permission Group Permissions
android.permission-group.CALENDAR
  • android.permission.READ_CALENDAR
  • android.permission.WRITE_CALENDAR
android.permission-group.CAMERA
  • android.permission.CAMERA
android.permission-group.CONTACTS
  • android.permission.READ_CONTACTS
  • android.permission.WRITE_CONTACTS
  • android.permission.GET_ACCOUNTS
android.permission-group.LOCATION
  • android.permission.ACCESS_FINE_LOCATION
  • android.permission.ACCESS_COARSE_LOCATION
android.permission-group.MICROPHONE
  • android.permission.RECORD_AUDIO
android.permission-group.PHONE
  • android.permission.READ_PHONE_STATE
  • android.permission.CALL_PHONE
  • android.permission.READ_CALL_LOG
  • android.permission.WRITE_CALL_LOG
  • com.android.voicemail.permission.ADD_VOICEMAIL
  • android.permission.USE_SIP
  • android.permission.PROCESS_OUTGOING_CALLS
android.permission-group.SENSORS
  • android.permission.BODY_SENSORS
android.permission-group.SMS
  • android.permission.SEND_SMS
  • android.permission.RECEIVE_SMS
  • android.permission.READ_SMS
  • android.permission.RECEIVE_WAP_PUSH
  • android.permission.RECEIVE_MMS
  • android.permission.READ_CELL_BROADCASTS
android.permission-group.STORAGE
  • android.permission.READ_EXTERNAL_STORAGE
  • android.permission.WRITE_EXTERNAL_STORAGE
注意:一個組的任何一個許可權被授權了,其他許可權會被自動授權。

例如:一旦WRITE_EXTERNAL_STORAGE被授權了,app也有READ_EXTERNAL_STORAGE許可權了。