[JAVA] When you receive a call, send an SMS to that number

What to add to AndroidManifest.xml

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />

As it is written,

Java

Description of the phone

TelephonyManager and PhoneStateListener do something about it.

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telephonyManager.listen(new PhoneStateListener(){
	@Override
	public void onCallStateChanged(int state, String number) {
		//The state is the number that indicates the state, and the number is the number that was received when the call was received.
		switch(state) {
			//Incoming event
			case TelephonyManager.CALL_STATE_RINGING:
				System.out.println(number + "It's an incoming call from Mr.");
				break;
			//Event at the start of a call
			case TelephonyManager.CALL_STATE_OFFHOOK:
				System.out.println("I started calling");
				break;
			//When changing to the standby state(When you hang up)Event
			case TelephonyManager.CALL_STATE_IDLE:
				System.out.println("I hung up the phone");
				break;
		}
	}
}, PhoneStateListener.LISTEN_CALL_STATE);

Description of SMS

SmsManager does a good job.

sendTextMessage method

Send text-based SMS. All I have to do is send an SMS, so I'll look into the details at another time.

argument
  1. String destinationAddress --Destination address
  2. String scAddress --Service center address to use the default SMSC ――Honestly, I don't really understand, so null
  3. String text --Message content
  4. PendingIntent sentIntent --Broadcast when a message is successfully sent or fails if not null --How to specify when you want detailed control
  5. PendingIntent deliveryIntent --Broadcast when the message is delivered to the recipient, if not null --Honestly, I don't know the difference from sentIntent, so I need to investigate.
String number = "09000000000";	//Destination phone number

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(
	number,
	null,
    "Hello",
    null,
    null
);

Final description

Send a simple message to the person who called you, just saying "Hey !!".

MainActivity.java


package example.com.sample003;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;

public class MainActivity extends AppCompatActivity {

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

        // -----The important thing is ↓ from here ↓----- //
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        telephonyManager.listen(new PhoneStateListener() {
            @Override
            public void onCallStateChanged(int state, String number) {
                //The state is the number that indicates the state, and the number is the number that was received when the call was received.
                switch (state) {
                    //Incoming event
                    case TelephonyManager.CALL_STATE_RINGING:
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(
                                number,
                                null,
                                "Hey!!",
                                null,
                                null
                        );
                        break;
                    //Event at the start of a call
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        break;
                    //When changing to the standby state(When you hang up)Event
                    case TelephonyManager.CALL_STATE_IDLE:
                        break;
                }
            }
        }, PhoneStateListener.LISTEN_CALL_STATE);
        // -----↑ Up to here ↑----- //
    }
}

bonus

Sample

Recommended Posts

When you receive a call, send an SMS to that number
What to do when you want to delete a migration file that is "NO FILE"
How to make a Vagrant Plugin that you learned when you forked and published vagrant-mutagen
An introductory book to read when you start Rails
Send a command to Spigot from an external process
A memo to check when you try to use Lombok
What to do when you launch an application with rails
How to change a string in an array to a number in Ruby
I want to call a method and count the number
[Swift] When you want to know if the number of characters in a String matches a certain number ...
Object-oriented design that can be used when you want to return a response in form format
What to do when an UnsupportedCharsetException occurs in a lightweight JRE
Setting that converts to binding.pry when you type pry in VScode
What to do when you become a Problem During Content Assist
What to do when you run into a docker-compose node_modules problem
Creating an ArrayList that allows you to throw in and retrieve the coordinates of a two-dimensional plane
Send a pull request to GitHub
[Swift] How to send a notification
A memo when you want to clear the time part of the calendar
When you want Rails to disable a session for a specific controller only
A program that determines whether the entered integer is close to an integer
We have released a service that allows you to easily create chats!
[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.
What to do if you get an error when you hit Heroku logs
A story about an arithmetic overflow that you shouldn't encounter in Ruby
What to do if you get an "A server is already running." Error when you try to start the rails server
Are you still exhausted by the sample video search? A button to send FANZA videos to Slack when pressed.
When introducing JOOQ to Spring boot, a story that was dealt with because an error occurred around Liquibase