Android---23---傳送簡訊

來源:互聯網
上載者:User

標籤:android   smsmanager   

SmsManager:管理簡訊操作

通過調用靜態方法 SmsManager.getDefault() 擷取此對象。 


SmsManager


方法摘要 
 boolean copyMessageToSim(byte[] smsc, byte[] pdu, int status) 
          Copy a raw SMS PDU to the SIM. 
 boolean deleteMessageFromSim(int messageIndex) 
          Delete the specified message from the SIM. 
 ArrayList<String> divideMessage(String text) 
          Divide a text message into several messages, none bigger than the maximum SMS message size. 
 ArrayList<SmsMessage> getAllMessagesFromSim() 
          Retrieves all messages currently stored on SIM. 
static SmsManager getDefault() 
          Get the default instance of the SmsManager 
 void sendDataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent 


sentIntent, PendingIntent deliveryIntent) 
          Send a data based SMS to a specific application port. 
 void sendMultipartTextMessage(String destinationAddress, String scAddress, ArrayList<String> parts, 


ArrayList<PendingIntent> sentIntents, ArrayList<PendingIntent> deliveryIntents) 
          Send a multi-part text based SMS. 
 void sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent 


deliveryIntent) 
          Send a text based SMS. 
 boolean updateMessageOnSim(int messageIndex, int newStatus, byte[] pdu) 
          Update the specified message on the SIM. 




manager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent);
  
destinationAddress  —— 訊息的目標地址   
scAddress  —— 服務中心的地址 or 為空白使用當前預設的 SMSC 3) 
text —— 訊息的主體,即訊息要發送的資料   
sentIntent  —— 如果不為空白,當訊息成功發送或失敗這個 PendingIntent 就廣播。結果代碼是 Activity.RESULT_OK 表示成功


,或 RESULT_ERROR_GENERIC_FAILURE 、 RESULT_ERROR_RADIO_OFF 、 RESULT_ERROR_NULL_PDU 之一表示錯誤。對應 


RESULT_ERROR_GENERIC_FAILURE , sentIntent  可能包括額外的 “ 錯誤碼 ” 包含一個無線電廣播技術特定的值,通常只在修複故障


時有用。   
 每一個基於 SMS 的應用程式控制檢測 sentIntent  。如果 sentIntent  是空,調用者將檢測所有未知的應用程式,這將導致在檢測的時


候發送較小數量的 SMS 。 
 deliveryIntent  —— 如果不為空白,當訊息成功傳送到接收者這個 PendingIntent 就廣播。 



MainActivity.java:


import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.telephony.gsm.SmsManager;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;/** * 傳送簡訊 *  * @author Caesar *  */public class MainActivity extends Activity implements OnClickListener {private EditText numText;private EditText contentText;private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);numText = (EditText) findViewById(R.id.number);contentText = (EditText) findViewById(R.id.content);button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubString number = numText.getText().toString();String content = contentText.getText().toString();// SmsManager:管理簡訊操作,通過靜態方法SmsManager.getDefault()擷取此對象SmsManager manager = SmsManager.getDefault();ArrayList<String> texts = manager.divideMessage(content);for (String text : texts) {manager.sendTextMessage(number, null, text, null, null);}Toast.makeText(MainActivity.this, "發送成功", Toast.LENGTH_LONG).show();}}


activity_main.xml:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    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="com.example.sendmessagedemo.MainActivity" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="輸入手機號" />    <EditText        android:id="@+id/number"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="輸入簡訊內容" />    <EditText        android:id="@+id/content"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:minLines="3" />    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="發送" /></LinearLayout>


需要添加許可權:


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





Android---23---傳送簡訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.