【Android】如何實現Android發送短信
阿新 • • 發佈:2017-11-13
ted param close ase find array 短信 red phone
第一種:調用系統短信接口直接發送短信;主要代碼如下:
/** * 直接調用短信接口發短信 * @param phoneNumber * @param message */ public void sendSMS(String phoneNumber,String message){ //獲取短信管理器 android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault(); smsManager.sendTextMessage(phoneNumber,null, message, sentPI, deliverPI); }
第二種:如果短信的長度超過了發送了發送限制,那麽可以使用如下的方法:
/** * 直接調用短信接口發短信 * @param phoneNumber * @param message */ public void sendMSM(String phoneNumber, String message) { android.telephony.SmsManager smsManager = android.telephony.SmsManager .getDefault(); ArrayList<String> divideContents = smsManager.divideMessage(message); ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); ArrayList<PendingIntent> deliverIntents = new ArrayList<PendingIntent>(); for (String text : divideContents) { sentIntents.add(sentPI); deliverIntents.add(deliverPI); } smsManager.sendMultipartTextMessage(phoneNumber,null, divideContents, sentIntents, deliverIntents); }
別忘了權限,記得在AndroidMannifest.xml中設置權限:
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
這種方法可以監控發送狀態和對方接收狀態。
處理返回的發送狀態:
//處理返回的發送狀態 String SENT_SMS_ACTION = "SENT_SMS_ACTION"; Intent sentIntent = new Intent(SENT_SMS_ACTION); PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0); // register the Broadcast Receivers context.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "短信發送成功", Toast.LENGTH_SHORT) .show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; } } }, new IntentFilter(SENT_SMS_ACTION));
處理返回的接收狀態 :
//處理返回的接收狀態 String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION"; // create the deilverIntent parameter Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION); PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0, deliverIntent, 0); context.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { Toast.makeText(context, "收信人已經成功接收", Toast.LENGTH_SHORT) .show(); } }, new IntentFilter(DELIVERED_SMS_ACTION));
發送短信參數說明:
smsManager.sendTextMessage(destinationAddress, scAddress, text, sentIntent, deliveryIntent)
-- destinationAddress:目標電話號碼
-- scAddress:短信中心號碼,測試可以不填
-- text: 短信內容
-- sentIntent:發送 -->中國移動 --> 中國移動發送失敗 --> 返回發送成功或失敗信號 --> 後續處理 即,這個意圖包裝了短信發送狀態的信息
-- deliveryIntent: 發送 -->中國移動 --> 中國移動發送成功 --> 返回對方是否收到這個信息 --> 後續處理 即:這個意圖包裝了短信是否被對方收到的狀態信息(供應商已經發送成功,但是對方沒有收到)。
接下來是一個Demo,android版本為4.4
首頁界面:
AndroidManifest.xml文件:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.smstest" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.smstest.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>androidManifest.xml
MainActivity.java
package com.example.smstest; import java.util.ArrayList; import java.util.List; import android.os.Bundle; import android.app.Activity; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.telephony.SmsManager; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private LinearLayout llt; private Context context; private int PHONENUMBER=1001; private int SMSCONTENT=1002; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); llt= (LinearLayout)findViewById(R.id.main_layout); context=this; addText("電話號碼"); addTextField(PHONENUMBER); addText("短信內容"); addTextField(SMSCONTENT); Button btn= addBtn("發送短信"); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { //處理返回的發送狀態 String SENT_SMS_ACTION = "SENT_SMS_ACTION"; Intent sentIntent = new Intent(SENT_SMS_ACTION); PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, sentIntent, 0); // register the Broadcast Receivers context.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { switch (getResultCode()) { case Activity.RESULT_OK: Toast.makeText(context, "短信發送成功", Toast.LENGTH_SHORT) .show(); break; case SmsManager.RESULT_ERROR_GENERIC_FAILURE: break; case SmsManager.RESULT_ERROR_RADIO_OFF: break; case SmsManager.RESULT_ERROR_NULL_PDU: break; } } }, new IntentFilter(SENT_SMS_ACTION)); //處理返回的接收狀態 String DELIVERED_SMS_ACTION = "DELIVERED_SMS_ACTION"; // create the deilverIntent parameter Intent deliverIntent = new Intent(DELIVERED_SMS_ACTION); PendingIntent deliverPI = PendingIntent.getBroadcast(context, 0, deliverIntent, 0); context.registerReceiver(new BroadcastReceiver() { @Override public void onReceive(Context _context, Intent _intent) { Toast.makeText(context, "收信人已經成功接收", Toast.LENGTH_SHORT) .show(); } }, new IntentFilter(DELIVERED_SMS_ACTION)); //獲取短信手機號碼 EditText ett= (EditText)llt.findViewById(PHONENUMBER); String phoneNumber= ett.getText().toString(); //獲得短信信息 EditText smsEtt=(EditText)llt.findViewById(SMSCONTENT); String smscontent=smsEtt.getText().toString(); //創建一個短信管理器 android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault(); ArrayList<String> divideContents= smsManager.divideMessage(smscontent); ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(); ArrayList<PendingIntent> deliverIntents = new ArrayList<PendingIntent>(); for(String text : divideContents){ sentIntents.add(sentPI); deliverIntents.add(deliverPI); } smsManager.sendMultipartTextMessage(phoneNumber, null, divideContents, sentIntents, deliverIntents); } }); } Button addBtn(String text){ Button btn=new Button(context); btn.setText(text); llt.addView(btn); return btn; } void addTextField(int id){ EditText ett=new EditText(context); ett.setId(id); llt.addView(ett); } void addText(String text){ TextView tv=new TextView(context); tv.setText(text); llt.addView(tv); } }MainActivity.java
【Android】如何實現Android發送短信