Android 發簡訊功能

來源:互聯網
上載者:User

標籤:

運行效果:  首先建立一個android project 在string.xml中定義需要用到的字串:
<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World, MainActivity!>    <string name="app_name">發簡訊程式</string>    <string name="str_input_phone_number">請輸入手機號</string>    <string name="str_input_sms_content">請輸入簡訊內容</string>    <string name="str_send_sms">傳送簡訊</string>    <string name="str_remind_input_phone_number">請輸入手機號</string>    <string name="str_remind_sms_send_finish">發送完成</string></resources>

在main.xml中編輯介面控制項代碼:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TextView      android:layout_width="fill_parent"      android:layout_height="wrap_content"      android:text="@string/str_input_phone_number"     />    <EditText        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/phone_number_editText"        />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/str_input_sms_content"        />            <EditText        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:id="@+id/sms_content_editText"        />            <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/str_send_sms"        android:id="@+id/send_sms_button"        />        </LinearLayout>

最後在MainActivity中編寫傳送簡訊的邏輯代碼:

package com.sms.ui;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                phone_number_editText = (EditText) findViewById(R.id.phone_number_editText);        sms_content_editText = (EditText) findViewById(R.id.sms_content_editText);        send_sms_button = (Button) findViewById(R.id.send_sms_button);                send_sms_button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                String phone_number = phone_number_editText.getText().toString().trim();                String sms_content = sms_content_editText.getText().toString().trim();                if(phone_number.equals("")) {                    Toast.makeText(MainActivity.this, R.string.str_remind_input_phone_number, Toast.LENGTH_LONG).show();                } else {                    SmsManager smsManager = SmsManager.getDefault();                    if(sms_content.length() > 70) {                        List<String> contents = smsManager.divideMessage(sms_content);                        for(String sms : contents) {                            smsManager.sendTextMessage(phone_number, null, sms, null, null);                        }                    } else {                     smsManager.sendTextMessage(phone_number, null, sms_content, null, null);                    }                    Toast.makeText(MainActivity.this, R.string.str_remind_sms_send_finish, Toast.LENGTH_SHORT).show();                }            }             });    }        private EditText phone_number_editText;    private EditText sms_content_editText;    private Button send_sms_button;}

在功能資訊清單檔中聲明發簡訊許可權:

Android 發簡訊功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.