java - 是否可以在 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);
}
}
}
解决方案
推荐阅读
- python - 理解 h 指数计算的术语
- spring - 如何使用 Spring 消息传递 RSocket @ConnectMapping 解析身份验证参数
- amazon-web-services - 具有两个负载均衡器的 AWS CodeDeploy(蓝/绿)部署组
- here-api - HERE 车队远程信息处理。例子。汽车拖车的错误?
- php - 工匠测试命令不起作用,laravel 7.x
- c# - 如何获取程序的当前文件夹名称并将zip文件解压缩到C#中的当前文件夹?
- javascript - 从两个链接打开 ant design popover
- flutter - 导致布局问题的列小部件
- r - R GGally::ggpairs,相关矩阵图,如何自定义诊断
- php - Redis 键名与 laravel 中定义的名称不匹配