大家好我是编程新手。下面的代码可以通过按下按钮预定义的SMS到预定义的数字,但它将带到消息编辑器屏幕,并要求我点击发送。如何在不转到消息编写器的情况下直接发送SMS。我看到很少有人问这个,其中一个解决方案是使用SMSmanger
,但我不知道如何在我的代码中使用SMSmanger
代码。
public class SendSMSActivity extends Activity {
Button buttonSend;
Button buttonSend2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonSend = (Button) findViewById(R.id.buttonSend);
buttonSend2 = (Button) findViewById(R.id.buttonSend2);
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonSend:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "#abc");
sendIntent.putExtra("address", "9900990");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
break;
case R.id.buttonSend2:
Intent sendIntent1 = new Intent(Intent.ACTION_VIEW);
sendIntent1.putExtra("sms_body", "#def");
sendIntent1.putExtra("address", "9900990");
sendIntent1.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent1);
break;
}
}
};
buttonSend.setOnClickListener(listener);
buttonSend2.setOnClickListener(listener);
}
)
谢谢
答案 0 :(得分:0)
尝试以下:
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
注意:它需要权限 android.permission.SEND_SMS
答案 1 :(得分:0)
MainActivity
EditText etPhone;
EditText etMessage;
Button button;
public String[] mPermission = {SEND_SMS, READ_SMS};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etPhone = findViewById(R.id.main_et_phone);
etPhone = findViewById(R.id.main_et_message);
button = findViewById(R.id.main_btn_send);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SendSMS();
}
});
}
private void SendSMS() {
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("SMS_SEND"), 0);
PendingIntent pendingIntentDel = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent("SMS_DELIVERED"), 0);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(etPhone.getText().toString(), null, etMessage.getText().toString(), pendingIntent, pendingIntentDel);
}
private void SMSPermission() {
ActivityCompat.requestPermissions(MainActivity.this, mPermission, 1);
}
@Override
protected void onResume() {
super.onResume();
SMSPermission();
}