0

大家好,我是编程新手。下面的代码可以通过按下按钮将短信预定义到预定义的号码,但它会进入消息编辑器屏幕并要求我点击发送。我如何直接发送短信而不去消息编辑器。我看到很少有人问这个问题,其中一个解决方案正在使用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);
}
)

谢谢

4

2 回答 2

0

尝试以下:

SmsManager sms = SmsManager.getDefault();  
sms.sendTextMessage(phoneNumber, null, message, null, null);

注意:它需要权限android.permission.SEND_SMS

于 2014-02-15T08:38:27.130 回答
0

主要活动

    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();
    }
于 2019-04-06T06:39:48.253 回答