android打电话方法(直接拨通)

古人学问无遗力,少壮工夫老始成。这篇文章主要讲述android打电话方法(直接拨通)相关的知识,希望能为你提供帮助。
新建了CallPhone方法,如下:

1 private void CallPhone() { 2String number = et_number.getText().toString(); 3if (TextUtils.isEmpty(number)) { 4// 提醒用户 5Toast.makeText(MainActivity.this, "我叫吐司,请填写号码,行不行!!!", Toast.LENGTH_SHORT).show(); 6} else { 7// 拨号:激活系统的拨号组件 8Intent intent = new Intent(); // 意图对象:动作 + 数据 9intent.setAction(Intent.ACTION_CALL); // 设置动作 10Uri data = https://www.songbingjia.com/android/Uri.parse("tel:" + number); // 设置数据 11intent.setData(data); 12startActivity(intent); // 激活Activity组件 13} 14}

要记得在清单文件androidManifest.xml中添加权限
1 < ?xml version="1.0" encoding="utf-8"?> 2 < manifest xmlns:android="http://schemas.android.com/apk/res/android" 3package="***"> 4 5< application 6...... 7< /application> 8< uses-permission android:name="android.permission.CALL_PHONE"/> 9 10 < /manifest>

为方便理解,给出activyti_main.xml:
1 < ?xml version="1.0" encoding="utf-8"?> 2 < android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:app="http://schemas.android.com/apk/res-auto" 4xmlns:tools="http://schemas.android.com/tools" 5android:layout_width="match_parent" 6android:layout_height="match_parent" 7tools:context=".MainActivity"> 8 9< LinearLayout 10android:layout_width="match_parent" 11android:layout_height="match_parent" 12android:orientation="vertical"> 13 14< EditText 15android:id="@+id/ed" 16android:layout_width="match_parent" 17android:layout_height="wrap_content" 18android:hint="请输入手机号" /> 19 20< Button 21android:id="@+id/button" 22android:layout_width="wrap_content" 23android:layout_height="wrap_content" 24android:text="拨打" /> 25< /LinearLayout> 26 27 28 < /android.support.constraint.ConstraintLayout>

在onCreate()中找到控件,给南牛添加点击事件,调用此方法即可。
顺便写一下从网上找到的android6.0动态获取权限的代码(亲测可用):
1 // 检查是否获得了权限(Android6.0运行时权限) 2//权限没有获得 3if (ContextCompat.checkSelfPermission(MainActivity.this, 4Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){ 5// 没有获得授权,申请授权 6if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 7Manifest.permission.CALL_PHONE)) { 8// 返回值: 9 //如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true. 10 //如果用户之前拒绝权限的时候勾选了对话框中” Don’ t ask again” 的选项,那么这个方法会返回false. 11 //如果设备策略禁止应用拥有这条权限, 这个方法也返回false. 12// 弹窗需要解释为何需要该权限,再次请求授权 13Toast.makeText(MainActivity.this, "请授权!", Toast.LENGTH_LONG).show(); 14 15// 帮跳转到该应用的设置界面,让用户手动授权 16Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); 17Uri uri = Uri.fromParts("package", getPackageName(), null); 18intent.setData(uri); 19startActivity(intent); 20}else{ 21// 不需要解释为何需要该权限,直接请求授权 22ActivityCompat.requestPermissions(MainActivity.this, 23new String[]{Manifest.permission.CALL_PHONE}, 24MY_PERMISSIONS_REQUEST_CALL_PHONE); 25} 26}else { 27// 已经获得授权,可以打电话 28CallPhone(); 29}

【android打电话方法(直接拨通)】 

    推荐阅读