Android studio case-realizing phone dialing
table of Contents
One, code configuration
1. Create a project
Flow chart
2. Add code
Change the layout
Layout the complete code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话拨号"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
Insert picture
activity_main_xml file layout
complete code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话拨号"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入电话号码"
android:inputType="number"
android:id="@+id/phoneNum"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/call"
android:layout_centerInParent="true"
android:id="@+id/call_btn"/>
</RelativeLayout>
</LinearLayout>
Effect display
mainactivity.java file
EditText phoneNum;
ImageButton call_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneNum=(EditText) findViewById(R.id.phoneNum);
call_btn=(ImageButton) findViewById(R.id.call_btn);
call_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phoneNum.getText()));
startActivity(intent);
Picture
Run code
Try to dial, an error occurs, you need to set the corresponding permissions
3. Permission request
Androidmainfest.xml file
<uses-permission android:name="android.permission.CALL_PHONE"/>
Android 6.0 and above need to manually grant permissions by yourself.
Application permission request
version number judgment method
protected boolean shouldAskPermissions(){
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}
How to apply for permission
protected void askPermissions() {
String[] permissions = {
"android.permission.CALL_PHONE"
};
int requestCode = 200;
requestPermissions(permissions, requestCode);
}
Call in onCreate
if(shouldAskPermissions()){
askPermissions();
}
When requesting permission to add code, make sure that the program is running, otherwise an error will be reported when the code is added.
Second, the effect demonstration
Enter the number randomly, and the prompt is a blank number.