Android如何实现Android发送短信
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android如何实现Android发送短信相关的知识,希望对你有一定的参考价值。
第一种:调用系统短信接口直接发送短信;主要代码如下:
/** * 直接调用短信接口发短信 * @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>
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); } }
以上是关于Android如何实现Android发送短信的主要内容,如果未能解决你的问题,请参考以下文章