Does android realize the function of software service hotline?


General software has functions such as complaint calls and service calls, which are relatively simple to implement, mainly record it, so as to avoid finding it when you use it in the future. First look at the renderings, because local videos cannot be directly uploaded, all the videos I recorded on my phone with gif software.

Insert picture description here
The enlightenment principle is almost jump to your mobile phone to dial, the code is as follows:

1. Permission of Android Mainfest page

The permission code is as follows:

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

Insert picture description here

2.Acitivity page

To avoid errors prompting ToastUtils when running, you can delete it directly if you don't need it, and import the dependent package if you don't delete it:

implementation 'com.github.mengpeng920223:ToastUtils:v1.0.3'

If you want to delete:
Insert picture description here

Modify phoneNumber directly to dial the number you need.

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

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.provider.Settings;
import android.view.View;

import com.mengpeng.mphelper.ToastUtils;

public class MainActivity extends AppCompatActivity {
    
    
    private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE =0 ;
    private String phoneNumber="10086";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startCallPhone(View view) {
    
    
        //判断Android版本是否大于23
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    
    
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);

            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
    
    
                ActivityCompat.requestPermissions(this, new String[]{
    
    Manifest.permission.CALL_PHONE},
                        0);
                return;
            } else {
    
    
                callPhone(phoneNumber);
            }
        } else {
    
    
            callPhone(phoneNumber);
            // 检查是否获得了权限(Android6.0运行时权限)
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
    
    
                // 没有获得授权,申请授权
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.CALL_PHONE)) {
    
    
                    // 返回值:
//                          如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
//                          如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
//                          如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
                    // 弹窗需要解释为何需要该权限,再次请求授权
                    ToastUtils.onWarnShowToast("Warn toast");

                    // 帮跳转到该应用的设置界面,让用户手动授权
                    Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                    Uri uri = Uri.fromParts("package", getPackageName(), null);
                    intent.setData(uri);
                    startActivity(intent);
                } else {
    
    
                    // 不需要解释为何需要该权限,直接请求授权
                    ActivityCompat.requestPermissions(this,
                            new String[]{
    
    Manifest.permission.CALL_PHONE},
                            MY_PERMISSIONS_REQUEST_CALL_PHONE);
                }
            } else {
    
    
                // 已经获得授权,可以打电话
                callPhone(phoneNumber);
            }
        }

    }

    private void callPhone(String phoneNumber) {
    
    

        Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneNumber));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    // 处理权限申请的回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    
    
        switch (requestCode) {
    
    
            case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
    
    
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    
    
                    // 授权成功,继续打电话
                    callPhone(phoneNumber);
                } else {
    
    
                    // 授权失败!
                }
                break;
            }
        }

    }

}

3. The xml file is relatively simple, just a Button click event

The onclick method in the Button is the startCallPhone method in the corresponding Activity.

<?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">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="136dp"
        android:text="Button"
        android:onClick="startCallPhone"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

The method is relatively simple, mainly to record, if you have any questions, you can bring it up.

Guess you like

Origin blog.csdn.net/qq_45137584/article/details/113100100