Android基礎開發小案例之簡訊發送器_Android

來源:互聯網
上載者:User

先看看效果圖:

布局檔案:
activity_main.xml

<span style="font-family:Comic Sans MS;font-size:14px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/tools"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:paddingBottom="@dimen/activity_vertical_margin"   android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin"   tools:context=".MainActivity" >    <TextView     android:id="@+id/textView1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentTop="true"     android:layout_centerHorizontal="true"     android:layout_marginTop="17dp"     android:text="請輸入手機號碼:"     android:textSize="20dp" />    <EditText     android:id="@+id/editText1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_below="@+id/textView1"     android:layout_centerHorizontal="true"     android:layout_marginTop="25dp"     android:background="@android:drawable/editbox_dropdown_light_frame"     android:ems="10"     android:inputType="phone"     android:singleLine="true" >      <requestFocus />   </EditText>    <TextView     android:id="@+id/textView2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignLeft="@+id/textView1"     android:layout_below="@+id/editText1"     android:layout_marginTop="39dp"     android:text="請輸入簡訊的內容:"     android:textSize="20dp" />    <EditText     android:id="@+id/editText2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_below="@+id/textView2"     android:layout_centerHorizontal="true"     android:layout_marginTop="49dp"     android:background="@android:drawable/editbox_background"     android:ems="10"     android:hint="發送的內容..."     android:inputType="textMultiLine"     android:lines="5" />    <Button     android:id="@+id/btn_send"     android:layout_width="50dp"     android:layout_height="30dp"     android:layout_below="@+id/editText2"     android:layout_centerHorizontal="true"     android:layout_marginTop="30dp"     android:background="@drawable/reply_send_button" />  </RelativeLayout></span> 

Java代碼:

<span style="font-family:Comic Sans MS;font-size:14px;">package com.bzu.gxs;  import java.util.ArrayList;  import android.os.Bundle; import android.app.Activity; import android.telephony.SmsManager; import android.text.TextUtils; import android.view.Menu; 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 implements OnClickListener {   private EditText et_number;   private EditText et_content;   private Button btn_send;    @Override   protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);     et_number = (EditText) findViewById(R.id.editText1);     et_content = (EditText) findViewById(R.id.editText2);     btn_send = (Button) findViewById(R.id.btn_send);      btn_send.setOnClickListener(this);   }    @Override   public void onClick(View v) {     switch (v.getId()) {     case R.id.btn_send:       // 擷取手機號碼       String number = et_number.getText().toString().trim();       // 擷取簡訊內容       String content = et_content.getText().toString().trim();       // 判斷手機和簡訊的內容是否為空白       if (TextUtils.isEmpty(content) || TextUtils.isEmpty(number)) {         Toast.makeText(MainActivity.this, "手機號 或 簡訊內容 為空白 ...",             Toast.LENGTH_LONG).show();         return;       } else {         SmsManager smsManger = SmsManager.getDefault();         // 把簡訊拆分成多個片段,防止簡訊內容過長,發送失敗         ArrayList<String> contents = smsManger.divideMessage(content);         // 遍曆簡訊內容         for (String str : contents) {           /*            * smsManger.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)            * sendTextMessage方法的            * 第一個參數是資訊的接收者            * 第二個參數是簡訊來自於哪裡,目前不支援填寫null就可以            * 第三個參數簡訊發送的內容            * 第四個參數是判斷簡訊是否發送成功            * 第五個參數是對面接收到你發的簡訊的一個訊息報告            */           smsManger               .sendTextMessage(number, null, content, null, null);           Toast.makeText(MainActivity.this, "發送成功...",               Toast.LENGTH_LONG).show();         }       }       break;     }   } } </span> 

以上就是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.