先上UI效果图
点击联系博主
拒绝权限
同意权限
直接拨号
跳转到拨号界面
跳转到拨号界面 同时附带号码
代码实现
AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.CALL_PHONE" />
点击联系博主
contactBlogger.setOnClickListener(v -> { callPhone(); // callUI(); // callPhoneUI(); });
判断是否有拨号权限
// 判断是否有拨号权限 private boolean ifHaveCallPhonePermission() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { // 动态申请权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE_REQUEST_CODE); return false; } return true; }
申请权限回调
// 申请权限回调 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == CALL_PHONE_REQUEST_CODE) { if (permissions.length != 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "请添加拨号权限后重试", Toast.LENGTH_SHORT).show(); } else { callPhone(); // callUI(); // callPhoneUI(); } } } // 申请
直接拨号
// 直接拨号 private void callPhone() { if (ifHaveCallPhonePermission()) { startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(phone))); } }
跳转到拨号界面
// 跳转到拨号界面 private void callUI() { if (ifHaveCallPhonePermission()) { startActivity(new Intent(Intent.ACTION_CALL_BUTTON)); } }
跳转到拨号界面 同时附带号码
// 跳转到拨号界面 同时附带号码 private void callPhoneUI() { if (ifHaveCallPhonePermission()) { startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(phone))); } }
完整代码
MainActivity.java
package top.gaojc.callphonedemo; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button contactBlogger; private final String phone = "tel:13103679960"; private final int CALL_PHONE_REQUEST_CODE = 10001;//拨号请求码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); contactBlogger = findViewById(R.id.contact_blogger); contactBlogger.setOnClickListener(v -> { callPhone(); // callUI(); // callPhoneUI(); }); } // 判断是否有拨号权限 private boolean ifHaveCallPhonePermission() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { // 动态申请权限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, CALL_PHONE_REQUEST_CODE); return false; } return true; } // 申请权限回调 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == CALL_PHONE_REQUEST_CODE) { if (permissions.length != 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "请添加拨号权限后重试", Toast.LENGTH_SHORT).show(); } else { callPhone(); // callUI(); // callPhoneUI(); } } } // 直接拨号 private void callPhone() { if (ifHaveCallPhonePermission()) { startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(phone))); } } // 跳转到拨号界面 private void callUI() { if (ifHaveCallPhonePermission()) { startActivity(new Intent(Intent.ACTION_CALL_BUTTON)); } } // 跳转到拨号界面 同时附带号码 private void callPhoneUI() { if (ifHaveCallPhonePermission()) { startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(phone))); } } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/contact_blogger" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/contact_blogger" /> </FrameLayout>