簡體   English   中英

通過Android App以編程方式將短信發送到特定號碼

[英]send sms to a particular number through Android App programmatically

其實我的概念是與android應用程序的代碼消息必須發送特定的號碼。 我的代碼是

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    buttonSend = (Button) findViewById(R.id.buttonSend);
    textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
    textSMS = (EditText) findViewById(R.id.editTextSMS);

    buttonSend.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            String phoneNo = textPhoneNo.getText().toString();
            String sms = textSMS.getText().toString();

            try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                String smsNumber = textPhoneNo.getText().toString();
                  String smsText = textSMS.getText().toString();
                  smsManager.sendTextMessage(smsNumber, null, smsText, null, null);
                Toast.makeText(getApplicationContext(), "SMS Sent!",
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                        "SMS faild, please try again later!",
                        Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }
    });
}

AndroidManifest.xml文件中所需的權限:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM