首页 > 解决方案 > 是否可以在 Android 来电时自动发送短信?

问题描述

我一直在试图弄清楚如何在他们的电话响铃时自动向拨打我电话的用户发送短信。在我接听电话之前,用户应该收到一条短信。文本消息将在单击按钮时正常发送,但是当我将按钮单击调用为button.performClick(); 按钮不点击。另外,如果我绕过按钮,短信将不会发送出去。如果您有其他可行的解决方案,请告诉我。

@RequiresApi(api = Build.VERSION_CODES.Q)
public class RedirectCalls extends Activity
{

PhoneCallListener phoneListener;
public String prefix;
Intent intent;
public EditText incomingCallNum;
public EditText newMessage;
public String message;
public String number;
public SmsManager mySmsManager;
public Button mySendBtn;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    incomingCallNum = findViewById(R.id.TextPhoneNum);
    newMessage = findViewById(R.id.OutTextMsg);
    mySendBtn = findViewById(R.id.smsSendBtn);

    prefix = "0" + incomingCallNum + "0";
    intent = new Intent();

    message = newMessage.getText().toString();
    number = incomingCallNum.getText().toString();
    mySmsManager = SmsManager.getDefault();

    phoneListener = phoneListener = new PhoneCallListener();
    //callforward(phoneNumber);
}

public void callforward(String callForwardString)
{
    phoneListener = new PhoneCallListener();
    TelephonyManager telephonyManager = (TelephonyManager)
            this.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

    Intent intentCallForward = new Intent(Intent.ACTION_CALL);
    prefix = Uri.encode(prefix);
    Uri mmiCode = Uri.fromParts("tel", prefix, incomingCallNum.getText().toString());//tried to pass 'callForwardString' as param, but got null
    intentCallForward.setData(mmiCode);
    startActivity(intentCallForward);

    Log.d("PHONE_NUM ", "onCreate: " + incomingCallNum);
}


private class PhoneCallListener extends PhoneStateListener
{
    private boolean isPhoneCalling = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber)
    {



        if (TelephonyManager.CALL_STATE_RINGING == state)
        {
            mySendBtn.performClick();
            //mySmsManager.sendTextMessage(number, null, message, null, null);
            // phone ringing
            Log.d("PHONE_RINGS", "onCallStateChanged: " + "Phone ringing");
        }

        if (TelephonyManager.CALL_STATE_OFFHOOK == state)
        {
            // active
            isPhoneCalling = true;
            //callforward(phoneNumber);//tried to forward call after answer...

            // testing for forward call after connection is made
            Intent intentCallForward = new Intent(Intent.ACTION_CALL);
            //String prefix = "*21*";
            prefix = Uri.encode(prefix);
            Uri mmiCode = Uri.fromParts("tel", prefix, incomingCallNum.getText().toString());//tried to pass 'callForwardString' as param, but got null
            intentCallForward.setData(mmiCode);
            startActivity(intentCallForward);
        }

        if (TelephonyManager.CALL_STATE_IDLE == state)
        {
            // run when class initial and phone call ended, need detect flag
            // from CALL_STATE_OFFHOOK
            if (isPhoneCalling)
            {
                // restart app
                Intent i = getBaseContext().getPackageManager()
                        .getLaunchIntentForPackage(getBaseContext().getPackageName());
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
                isPhoneCalling = false;
            }
        }
    }
    public void sendSMS(View view) {
        message = newMessage.getText().toString();
        number = incomingCallNum.getText().toString();
        mySmsManager = SmsManager.getDefault();
        mySmsManager.sendTextMessage(number, null, message, null, null);
   }
  }
 }

标签: javastatelistenertelephonymanagersmsmanager

解决方案


推荐阅读