Texting and calling

Let's make an interface like this

 

 

1. Click the button to call up the interface of calling and texting

2. Long press button to dial and send SMS directly

We bind click monitoring on the interface

<Button
                android:id="@+id/btn_call"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Call up"
                android:onClick="jumpCall"/>
            <Button
                android:id="@+id/btn_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sending SMS"
                android:onClick="jumpSendMessage"/>

 

By implementing the OnLongClickListener interface, bind long press listener

 

Then bind

btn_call.setOnLongClickListener(this);
btn_sms.setOnLongClickListener(this);

 

Then the implementation method is enough. Note here that after Android 6.0, in addition to declaring permissions, you need to dynamically apply for permissions

if (checkSelfPermission(permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
    // TODO: Consider calling
    //    Activity#requestPermissions
    // here to request the missing permissions, and then overriding
    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
    //                                          int[] grantResults)
    // to handle the case where the user grants the permission. See the documentation
    // for Activity#requestPermissions for more details.
    ActivityCompat.requestPermissions(this, new String[]{permission.SEND_SMS}, 1);
    //ActivityCompat.shouldShowRequestPermissionRationale(this, permission.CALL_PHONE);
    return;
}

 

Give a complete interface layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="20dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Please enter the phone number:">

            </TextView>
            <EditText
                android:id="@+id/editText_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Please enter the phone number"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Please enter the message:"/>
            <EditText
                android:id="@+id/editText_message"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Enter SMS content here"/>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btn_call"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Call up"
                android:onClick="jumpCall"/>
            <Button
                android:id="@+id/btn_sms"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Sending SMS"
                android:onClick="jumpSendMessage"/>
        </LinearLayout>


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
layout

 

Complete mainActivity.java

package com.example.call_msm;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

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

import static android.Manifest.*;

public class MainActivity extends AppCompatActivity
        implements View.OnLongClickListener {

    private Button btn_call;
    private Button btn_sms;
    private EditText editText_number;
    private EditText editText_message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_call = findViewById(R.id.btn_call);
        btn_sms = findViewById(R.id.btn_sms);
        btn_call.setOnLongClickListener(this);
        btn_sms.setOnLongClickListener(this);
        editText_number = findViewById(R.id.editText_number);
        editText_message = findViewById(R.id.editText_message);
    }


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public boolean onLongClick(View v) {
        if (v.equals(btn_call)) {
            makeCall();
        } else if (v.equals(btn_sms)) {
            sendMessage();
        }

        return true;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void sendMessage() {
        if (checkSelfPermission(permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    Activity#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            ActivityCompat.requestPermissions(this, new String[]{permission.SEND_SMS}, 1);
            //ActivityCompat.shouldShowRequestPermissionRationale(this, permission.CALL_PHONE);
            return;
        }
        Toast.makeText(this, "Long press to send SMS", Toast.LENGTH_SHORT).show();
        String phoneNumber = editText_number.getText().toString();
        Uri uri = Uri.parse("smsto:" + phoneNumber);
        Intent intent = new Intent(Intent.ACTION_SEND, uri);
        String msg = editText_message.getText().toString();
        intent.putExtra("sms_body", msg);
        startActivity(intent);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void makeCall() {
        Toast.makeText(this, "Long press to call", Toast.LENGTH_SHORT).show();
        String phoneNumber = editText_number.getText().toString();
        Uri uri = Uri.parse("tel:" + phoneNumber);
        Intent intent = new Intent(Intent.ACTION_CALL, uri);
        if (checkSelfPermission(permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    Activity#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for Activity#requestPermissions for more details.
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CALL_PHONE},1);
            //ActivityCompat.shouldShowRequestPermissionRationale(this, permission.CALL_PHONE);
            return;
        }
        startActivity(intent);
    }

    public void jumpCall(View view) {
        Toast.makeText(this,"Phone",Toast.LENGTH_SHORT).show();
        String phoneNumber = editText_number.getText().toString();
        Uri uri = Uri.parse("tel:"+phoneNumber);
        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
        startActivity(intent);

    }

    public void jumpSendMessage(View view) {
        Toast.makeText(this,"Send message",Toast.LENGTH_SHORT).show();
        String phoneNumber = editText_number.getText().toString();
        Uri uri = Uri.parse("smsto:" + phoneNumber);
        Intent intent = new Intent(Intent.ACTION_SENDTO,uri);
        String msg = editText_message.getText().toString();
        intent.putExtra("sms_body",msg);
        startActivity(intent);
    }
}
mainActivity.java

Tags: Android Java xml encoding

Posted on Tue, 14 Apr 2020 08:16:26 -0700 by Boz