[android] record of call dial function implementation
I remember dial and call wrongly in the exam a few days ago, so I write a small demo here to make a difference and deepen my impression.
It is mainly used to realize the call function, and the dial function is used for comparison, with few words and code.
1. Create a layout file as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/btn_dial"
android:text="Dial"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/call"
android:text="call"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
layout file
That is to say, two buttons, DIAL and CALL, are added
2. Add Java code:

package com.cnblogs.dialandcall;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_dial;
private Button btn_call;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_call = (Button)findViewById(R.id.btn_call);
btn_call.setOnClickListener(this);
btn_dial = (Button)findViewById(R.id.btn_dial);
btn_dial.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_call:
onCall();
break;
case R.id.btn_dial:
Intent dialIntent = new Intent(Intent.ACTION_DIAL);
dialIntent.setData(Uri.parse("tel:10086"));
startActivity(dialIntent);
break;
}
}
private void onCall() {
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if(permissionCheck!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE}, Integer.parseInt("001"));
}
else{
startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:10086")));
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 001:
if(grantResults.length>0&&(grantResults[0]==PackageManager.PERMISSION_GRANTED)){
onCall();
}
else {
Toast.makeText(getBaseContext(),"You Need Allow The Permission To Run This App",Toast.LENGTH_SHORT).show();
}
break;
}
}
}
java file
- It should be noted that I added a separate method to the BTN call button click event for processing. This is because call phone is considered to be a dangerous permission in Android 6.0 and above, and it needs to be applied when the program is running.
- For the classification of permissions in Android, please refer to the following links:
https://developer.android.google.cn/guide/topics/security/permissions.html#normal-dangerous
3. Add the code of the Manifest.xml file:
<uses-permission android:name="android.permission.CALL_PHONE" />
Don't forget to add a permission statement to Android manifest.xml:)
Screenshot of implementation effect:
Screenshot 1. Click the CALL button to pop up the prompt box screenshot 2. Click the confirm button to directly jump to the CALL interface

Screenshot 3. Click DIAL to enter the dialing interface
Postscript:
A novice, welcome to exchange and correct!
thx : )