Android one touch dial

1. Title

Android one touch dial

2. Environment

AndroidStudio

Three, code implementation

SharedPreference stores phone numbers

This link stores the phone number for SharedPreference. It can be used together with one-touch dial.
If you don't use SharedPreference, use the reading method corresponding to the way you store the phone number to read the phone number.

package com.example.children;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

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

import java.util.ArrayList;
import java.util.List;


public class YiJianBoHuaActivity extends AppCompatActivity {
    
    
    private Button btnPhone1;
    private Button btnPhone2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yijianbohua);
        btnPhone1 = (Button) findViewById(R.id.btn_call_phone1);
        btnPhone2 = (Button) findViewById(R.id.btn_call_phone2);
        List<String> permissionList = new ArrayList<>();
        if(ContextCompat.checkSelfPermission(YiJianBoHuaActivity.this,Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
    
    
            permissionList.add(Manifest.permission.CALL_PHONE);
        }
        if(!permissionList.isEmpty()){
    
    
            String[] permissions = permissionList.toArray(new String[permissionList.size()]);
            ActivityCompat.requestPermissions(YiJianBoHuaActivity.this, permissions, 1);
        }else{
    
    
            Call1();
            Call2();
        }

    }
    public void Call1(){
    
    
        btnPhone1.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                SharedPreferences sp = getSharedPreferences("phone_info",MODE_PRIVATE);
                String phone1 = sp.getString("PhoneNumber1","");
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                        + phone1));
                if (ActivityCompat.checkSelfPermission(YiJianBoHuaActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
    
    
                    return;
                }
                startActivity(intent);

            }
        });
    }

    public void Call2(){
    
    
        btnPhone2.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                SharedPreferences sp = getSharedPreferences("phone_info",MODE_PRIVATE);
                String phone2 = sp.getString("PhoneNumber2","");
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                        + phone2));
                if (ActivityCompat.checkSelfPermission(YiJianBoHuaActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
    
    
                    return;
                }
                startActivity(intent);

            }
        });
    }
    /*只有同意打开相关权限才可以开启本程序*/
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    
    
        switch (requestCode) {
    
    
            case 1:
                if (grantResults.length > 0) {
    
    
                    for (int result : grantResults) {
    
    
                        if (result != PackageManager.PERMISSION_GRANTED) {
    
    
                            Toast.makeText(this, "必须同意所有权限才能使用本程序", Toast.LENGTH_SHORT).show();
                            finish();
                            return;
                        }
                    }
                    Call1();
                    Call2();

                } else {
    
    
                    Toast.makeText(this, "发生未知错误", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
        }
    }

}



Four, experiment screenshots

1

The interface is ugly, so bear with it.

If you have any questions, please leave a message to exchange!

Guess you like

Origin blog.csdn.net/weixin_43752257/article/details/113044511