android应用调用系统拨打电话
程序员文章站
2022-03-11 23:21:07
调用系统拨打电话功能,写了一个用例在AndroidManifest.xml中配置所需要的权限 CallActivity.javapackage com.example.a20200712;import android.Manifest;import android.content.Intent;impor....
调用系统拨打电话功能,写了一个用例 在AndroidManifest.xml中配置所需要的权限 <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> CallActivity.java
package com.example.a20200712;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class CallActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.call_layout);
editText = findViewById(R.id.call_layout_number);
}
public void calltel(View view){
String number = editText.getText().toString();
Log.i("mw","===="+number);
//android6版本获取动态权限
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
String[] permissions = {Manifest.permission.CALL_PHONE};
//验证是否许可权限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申请权限
this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
return;
}
}
}
//如果需要手动拨号将Intent.ACTION_CALL改为Intent.ACTION_DIAL(跳转到拨号界面,用户手动点击拨打)
Intent intent = new Intent(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:" +number);
intent.setData(data);
startActivity(intent);
}
}
call_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/call_layout_number"
android:hint="请输入手机号">
</EditText>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:onClick="calltel"
android:text="拨打电话"></Button>
</LinearLayout>
本文地址:https://blog.csdn.net/m0_37622302/article/details/107419667