android傳送簡訊填入手機號碼,6.0動態請求許可權撥打電話
阿新 • • 發佈:2019-01-22
//發簡訊填入號碼
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-group.CAMERA |
|
android.permission-group.CONTACTS |
|
android.permission-group.LOCATION |
|
android.permission-group.MICROPHONE |
|
android.permission-group.PHONE |
|
android.permission-group.SENSORS |
|
android.permission-group.SMS |
|
android.permission-group.STORAGE |
|
例如:一旦WRITE_EXTERNAL_STORAGE被授權了,app也有READ_EXTERNAL_STORAGE許可權了。