Android 基础-2.0 拔打电话号码

Android 基础-2.0 拔打电话号码,第1张

概述1、添加权限 在AndroidManifest.xml 添加打电话权限 <uses-permission android:name="android.permission.CALL_PHONE"/> 2、自动布局设置页面 <?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayou

1、添加权限

在AndroIDManifest.xml 添加打电话权限

<uses-permission androID:name="androID.permission.CALL_PHONE"/>

2、自动布局设置页面

<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.ConstraintLayout 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">    <TextVIEw        androID:ID="@+ID/tv_phone"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_margintop="10dp"        androID:layout_marginleft="10dp"        androID:text="@string/textPhone"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_totopOf="parent" />    <EditText        androID:ID="@+ID/et_phone"        androID:layout_wIDth="0dp"        androID:layout_height="wrap_content"        androID:layout_marginleft="10dp"        androID:layout_margintop="10dp"        androID:layout_marginRight="10dp"        androID:hint="@string/textPhoneHint"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_toBottomOf="@+ID/tv_phone" />    <button        androID:ID="@+ID/btn_call"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_marginStart="10dp"        androID:layout_marginleft="10dp"        androID:layout_margintop="16dp"        androID:text="@string/textPhonebutton"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_toBottomOf="@+ID/et_phone" /></androIDx.constraintlayout.Widget.ConstraintLayout>
VIEw Code

页面效果

3、给Activity添加拔打代码

public class MainActivity extends  BaseActivity {    private button eBtCall;    private EditText mEtPhone;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        eBtCall = findVIEwByID(R.ID.btn_call);        mEtPhone = findVIEwByID(R.ID.et_phone);        eBtCall.setonClickListener(new VIEw.OnClickListener() {            @OverrIDe            public voID onClick(VIEw v) {                performCodeWithPermission("拔打电话权限",new PermissionCallback() {                    @OverrIDe                    public voID hasPermission() {                        Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + mEtPhone.getText()));                        startActivity(intent);                    }                    @OverrIDe                    public voID nopermission() {                    }                },Manifest.permission.CALL_PHONE);            }        });    }}
VIEw Code

4、这里要注意,安卓在6.0之后是动态申请系统权限,因而封装一个权限的BaseActivity类,专门用于处理权限相关的。

package com.jiangys.telephonedial;import androID.content.DialogInterface;import androID.content.pm.PackageManager;import androID.os.Build;import androID.Widget.Toast;import androIDx.annotation.NonNull;import androIDx.appcompat.app.AlertDialog;import androIDx.appcompat.app.AppCompatActivity;import androIDx.core.app.ActivityCompat;/** * @author admin * @version $Rev$ * @des ${Todo} * @updateAuthor $Author$ * @updateDes ${Todo} */public class BaseActivity extends AppCompatActivity {    //**************** AndroID M Permission (AndroID 6.0权限控制代码封装)    private int permissionRequestCode = 88;    private PermissionCallback permissionRunnable;    public interface PermissionCallback {        voID hasPermission();        voID nopermission();    }    /**     * AndroID M运行时权限请求封装     *     * @param permissionDes 权限描述     * @param runnable      请求权限回调     * @param permissions   请求的权限(数组类型),直接从Manifest中读取相应的值,比如Manifest.permission.WRITE_CONTACTS     */    public voID performCodeWithPermission(@NonNull String permissionDes,PermissionCallback runnable,@NonNull String... permissions) {        if (permissions == null || permissions.length == 0)            return;        //        this.permissionrequestCode = requestCode;        this.permissionRunnable = runnable;        if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.M) || checkPermissionGranted(permissions)) {            if (permissionRunnable != null) {                permissionRunnable.hasPermission();                permissionRunnable = null;            }        } else {            //permission has not been granted.            requestPermission(permissionDes,permissionRequestCode,permissions);        }    }    private boolean checkPermissionGranted(String[] permissions) {        boolean flag = true;        for (String p : permissions) {            if (ActivityCompat.checkSelfPermission(this,p) != PackageManager.PERMISSION_GRANTED) {                flag = false;                break;            }        }        return flag;    }    private voID requestPermission(String permissionDes,final int requestCode,final String[] permissions) {        if (shouldShowRequestPermissionRationale(permissions)) {            /*1. 第一次请求权限时,用户拒绝了,下一次:shouldShowRequestPermissionRationale()  返回 true,应该显示一些为什么需要这个权限的说明            2.第二次请求权限时,用户拒绝了,并选择了“不在提醒”的选项时:shouldShowRequestPermissionRationale()  返回 false            3. 设备的策略禁止当前应用获取这个权限的授权:shouldShowRequestPermissionRationale()  返回 false*/            // ProvIDe an additional rationale to the user if the permission was not granted            // and the user would benefit from additional context for the use of the permission.            // For example,if the request has been denIEd prevIoUsly.            //            Snackbar.make(getwindow().getDecorVIEw(),requestname,//                    Snackbar.LENGTH_INDEFINITE)            //                    .setAction(R.string.common_ok,new VIEw.OnClickListener() {            //                        @OverrIDe            //                        public voID onClick(VIEw vIEw) {            //                            ActivityCompat.requestPermissions(BaseAppCompatActivity.this,//                                    permissions,//                                    requestCode);            //                        }            //                    })            //                    .show();            //如果用户之前拒绝过此权限,再提示一次准备授权相关权限            new AlertDialog.Builder(this)                    .setTitle("提示")                    .setMessage(permissionDes)                    .setPositivebutton("授权",new DialogInterface.OnClickListener() {                        @OverrIDe                        public voID onClick(DialogInterface dialog,int which) {                            ActivityCompat.requestPermissions(BaseActivity.this,permissions,requestCode);                        }                    }).show();        } else {            // Contact permissions have not been granted yet. Request them directly.            ActivityCompat.requestPermissions(BaseActivity.this,requestCode);        }    }    private boolean shouldShowRequestPermissionRationale(String[] permissions) {        boolean flag = false;        for (String p : permissions) {            if (ActivityCompat.shouldShowRequestPermissionRationale(this,p)) {                flag = true;                break;            }        }        return flag;    }    /**     * Callback received when a permissions request has been completed.     */    @OverrIDe    public voID onRequestPermissionsResult(int requestCode,@NonNull String[] permissions,@NonNull int[] grantResults) {        if (requestCode == permissionRequestCode) {            if (verifyPermissions(grantResults)) {                if (permissionRunnable != null) {                    permissionRunnable.hasPermission();                    permissionRunnable = null;                }            } else {                Toast.makeText(this,"暂无权限执行相关 *** 作!",Toast.LENGTH_SHORT).show();                if (permissionRunnable != null) {                    permissionRunnable.nopermission();                    permissionRunnable = null;                }            }        } else {            super.onRequestPermissionsResult(requestCode,grantResults);        }    }    public boolean verifyPermissions(int[] grantResults) {        // At least one result must be checked.        if (grantResults.length < 1) {            return false;        }        // Verify that each required permission has been granted,otherwise return false.        for (int result : grantResults) {            if (result != PackageManager.PERMISSION_GRANTED) {                return false;            }        }        return true;    }    //********************** END AndroID M Permission ****************************************}
VIEw Code 总结

以上是内存溢出为你收集整理的Android 基础-2.0 拔打电话号码全部内容,希望文章能够帮你解决Android 基础-2.0 拔打电话号码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1129523.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存