Android-一键发送消息

时间:2018-09-26 03:16:00

标签: android sms

我正在尝试单击我的应用程序的单击按钮,以将预定义的消息发送到预定义的数字。

  import android.app.Activity;
  import android.view.View.OnClickListener;
  import android.os.Bundle;
  import android.telephony.SmsManager;
  import android.view.View;
  import android.widget.Button;
  import android.widget.Toast;

  public class SendSMSActivity extends Activity {


 Button Send;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send_sms);

    Send = (Button) findViewById(R.id.Send);

    buttonSend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String phoneNo = "+0101795123456";
            String mssg = "It is working!";

            try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, mssg, null,      null);
                Toast.makeText(getApplicationContext(), "SMS    Sent!"+phoneNo,
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                        "SMS failed!"+phoneNo,
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }

        }
    });

  }
}

只是收到了吐司消息而没有发送,这是在实际的电话上而不是模拟器上进行尝试,任何人都可以帮助我指出正确的方向。

3 个答案:

答案 0 :(得分:1)

将其放在您的清单中

<uses-permission android:name="android.permission.SEND_SMS"/>

答案 1 :(得分:1)

我建议使用图书馆:https://github.com/klinker41/android-smsmms

实现的实现

compile 'com.klinkerapps:android-smsmms:5.1.0'

首先,创建一个设置对象,其中包含您想要做的所有必要信息。如果您未设置任何内容,则只会将其设置为默认值,并且该功能可能无法使用。例如,如果您需要MMS,请设置MMSC,代理和端口,否则每次都会出现错误。

Settings sendSettings = new Settings();

下一步,将该设置对象附加到发件人

Transaction sendTransaction = new Transaction(mContext, sendSettings);
Now, create the Message you want to send

Message mMessage = new Message(textToSend, addressToSendTo);
mMessage.setImage(mBitmap);   // not necessary for voice or sms messages

然后您要做的就是发送消息

sendTransaction.sendNewMessage(message, threadId)

注意:threadId可以为空(使用Transaction.NO_THREAD_ID),但这有时会导致创建新线程,而不是将消息添加到现有线程中

如果要发送彩信,请确保将其添加到清单中:

<service android:name="com.android.mms.transaction.TransactionService"/>

示例功能

 public void sendMessage() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                com.klinker.android.send_message.Settings sendSettings = new com.klinker.android.send_message.Settings();
                sendSettings.setMmsc(settings.getMmsc());
                sendSettings.setProxy(settings.getMmsProxy());
                sendSettings.setPort(settings.getMmsPort());
                sendSettings.setUseSystemSending(true);

                Transaction transaction = new Transaction(MainActivity.this, sendSettings);

                Message message = new Message(messageField.getText().toString(), toField.getText().toString());

                if (imageToSend.isEnabled()) {
                    message.setImage(BitmapFactory.decodeResource(getResources(), R.drawable.android));
                }

                transaction.sendNewMessage(message, Transaction.NO_THREAD_ID);
            }
        }).start();
    }

答案 2 :(得分:1)

嗨,我已经创建了这种应用程序,请仔细阅读以下代码并相应地更新您的代码。

我的MainActivity类

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    // Constants
    private static final String TAG = MainActivity.class.getSimpleName();
    private static final int REQUEST_PERMISSION_SEND_SMS = 400;

    // UI  Components
    private EditText mEtMobileNumber, mEtMessage;
    private Button mBtnSendMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        mEtMobileNumber = findViewById(R.id.activity_main_et_mobile_number);
        mEtMessage = findViewById(R.id.activity_main_et_message);

        mBtnSendMessage = findViewById(R.id.activity_main_btn_send_message);
        mBtnSendMessage.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_main_btn_send_message:
                checkRuntimePermissionForSMS();
                break;
            default:
                break;
        }
    }


    private void checkRuntimePermissionForSMS(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, REQUEST_PERMISSION_SEND_SMS);
        }else{
            sendMessageFunction(mEtMobileNumber.getText().toString(), mEtMessage.getText().toString());
        }
    }

    private void sendMessageFunction(String phoneNo, String message) {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null, message, null, null);
        Toast.makeText(getApplicationContext(), "SMS sent.",
                Toast.LENGTH_LONG).show();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PERMISSION_SEND_SMS: {
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    sendMessageFunction(mEtMobileNumber.getText().toString(), mEtMessage.getText().toString());
                } else {
                    Toast.makeText(getApplicationContext(), "SMS faild, please try again.", Toast.LENGTH_LONG).show();
                    return;
                }
            }
        }

    }
}

我经许可添加的清单文件

<uses-permission android:name="android.permission.SEND_SMS" />

就这样,这对我有用

如果您需要更多帮助,请在此答案中添加评论,我会将示例项目上传到GitHub并与您共享。

谢谢!