调用系统拨打电话功能,写了一个用例
在AndroidManifest.xml中配置所需要的权限
CallActivity.java
package com.example.a;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import .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
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/call_layout_number"
android:hint="请输入手机号">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:onClick="calltel"
android:text="拨打电话">
本文地址:/m0_37622302/article/details/107419667
希望与广大网友互动??
点此进行留言吧!