Cover Image for Simple Caller Talker in Android
98 views

Simple Caller Talker in Android

Creating a simple caller talker app in Android involves making a phone call and then using Text-to-Speech (TTS) to speak the caller’s name or a predefined message. Below is a step-by-step guide on how to create a basic version of this app.

1. Set Up a New Android Project:

Start by creating a new Android project in Android Studio.

2. Design the User Interface (UI):

Design your app’s user interface. In this basic example, you can have a single button to initiate the phone call and TTS.

XML
<Button
    android:id="@+id/callButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Call and Talk"
    android:onClick="makeCallAndTalk" 
/>

3. Add TTS Permissions:

In your AndroidManifest.xml, add the necessary permissions for TTS.

XML
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

4. Implement the Java Code:

In your activity class, implement the functionality to make a phone call and use TTS. Here’s a simplified example:

Java
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.util.Locale;

public class MainActivity extends AppCompatActivity implements OnInitListener {

    private static final int PERMISSION_REQUEST_CODE = 123;
    private Button callButton;
    private TextToSpeech textToSpeech;

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

        callButton = findViewById(R.id.callButton);

        // Initialize TextToSpeech
        textToSpeech = new TextToSpeech(this, this);

        // Set up click listener for the call button
        callButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                makeCallAndTalk();
            }
        });
    }

    @Override
    public void onInit(int status) {
        if (status == TextToSpeech.SUCCESS) {
            int result = textToSpeech.setLanguage(Locale.US);

            if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                Toast.makeText(this, "Language not supported", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(this, "TextToSpeech initialization failed", Toast.LENGTH_SHORT).show();
        }
    }

    public void makeCallAndTalk() {
        // Check for CALL_PHONE permission
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, PERMISSION_REQUEST_CODE);
        } else {
            // You can make the call here
            String phoneNumber = "1234567890"; // Replace with the desired phone number
            String message = "Hello, this is a test call.";

            // Initiate the phone call
            makePhoneCall(phoneNumber);

            // Use TTS to speak the message
            textToSpeech.speak(message, TextToSpeech.QUEUE_FLUSH, null, null);
        }
    }

    private void makePhoneCall(String phoneNumber) {
        String uri = "tel:" + phoneNumber;
        startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(uri)));
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQUEST_CODE) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                makeCallAndTalk();
            } else {
                Toast.makeText(this, "Permission denied. Cannot make a call.", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (textToSpeech != null) {
            textToSpeech.stop();
            textToSpeech.shutdown();
        }
    }
}

5. Test the App:

Run your app on a physical device. When you click the “Call and Talk” button, the app should initiate a phone call and use TTS to speak the predefined message.

Remember to replace "1234567890" and "Hello, this is a test call." with the actual phone number and message you want to use. Also, handle permissions and permission requests appropriately for your target Android version.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS