mardi 10 août 2021

How do you read a text programmatically if someone text you the letter Y for an response Android?

I am trying to add an if statement to where something happens in my app that I am building that occurs only when someone texts back with the letter Y. What would be the best way to retrieve the letter Y on the text message and put it into an if statement condition?

I have included my code for you to see what I am trying to achieve.

@RequiresApi(api = Build.VERSION_CODES.Q)
public class TextManager extends AppCompatActivity{

PhoneCallListenerMessageSend phoneListenerSend;
PhoneCallListenerMessageReceive phoneListenerReceive;
Intent intent;

// public string
public static String Response_SMS;  // used for the string that comes in on a text message 
                                    // that says "Y".

// private static int
private static final int MY_PERMISSIONS_READ_SMS = 1;
private static final int MY_PERMISSIONS_SEND_SMS = 1;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    intent = new Intent();
    phoneListenerSend = new PhoneCallListenerMessageSend();
    phoneListenerReceive = new PhoneCallListenerMessageReceive();

    // self check permissions at runtime
    checkSelfPermission(Manifest.permission.READ_SMS);
    checkSelfPermission(Manifest.permission.SEND_SMS);

    // request permissions if necessary
    if (checkSelfPermission(Manifest.permission.READ_SMS)
            != PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[]{Manifest.permission.READ_SMS}, 
        MY_PERMISSIONS_READ_SMS);
        return;
    }
    if (checkSelfPermission(Manifest.permission.SEND_SMS)
            != PackageManager.PERMISSION_GRANTED){
        requestPermissions(new String[]{Manifest.permission.SEND_SMS}, 
        MY_PERMISSIONS_SEND_SMS);
        return;
    }
}

// handle the permissions request response switch
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] 
grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch(requestCode) {
        case MY_PERMISSIONS_READ_SMS: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permissions are granted
            } else {
                // permissions are denied
            }
            return;
        }
    }
    switch (requestCode) {
        case MY_PERMISSIONS_SEND_SMS: {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // permissions are granted
            } else {
                // permissions are denied
            }
            return;
        }
    }
}

// calling state is ringing - SEND_SMS
 private class PhoneCallListenerMessageSend extends PhoneStateListener {

    public void sendLongSMS(int state) {
        if (TelephonyManager.CALL_STATE_RINGING == state) {
            String phoneNum = "1234567890";
            String message = "The user you are trying to call is unavailable" 
             + "Text Y To Continue.";
            SmsManager smsManager = SmsManager.getDefault();
            ArrayList<String> parts = smsManager.divideMessage(message);
            smsManager.sendMultipartTextMessage(phoneNum, null, parts, null, null);
        }
    }
 }

 // while calling state is still ringing - READ_SMS
 private class PhoneCallListenerMessageReceive extends PhoneStateListener {
        public void receiveLongSMS(int state) {
            if (TelephonyManager.CALL_STATE_RINGING == state) {
                if(Response_SMS) {  // if statement is to have the string of the letter Y from 
                                    // the text message.
                    
              }
           }
        }
     }
  }

Aucun commentaire:

Enregistrer un commentaire