Android 发短信功能实现

在自己的应用增加发短信功能有两种方式:

1,跳转到系统短信页面发送,调用如下代码即可。

Intent intent = new Intent(Intent.ACTION_SENDTO, Uri

.parse("smsto:" + telnum));

                // 如果需要将内容传过去增加如下代码

       intent .putExtra("sms_body", body);  
        startActivity(intent);

2,调用系统发短信接口sendTextMessage。activity如下:

布局文件就是两个输入框和一个发送按钮。

AndroidManifest.xml需要权限
 

public class MainTestActivity extends Activity {

private static final String SENT_SMS_ACTION = "demo_sms_send_action";
private static final String KEY_PHONENUM = "phone_num";
private EditText mPhoneNumberEdit;
private EditText mContentEdit;
private Button mSendBtn;
protected Context mContext;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_test);
mContext = this;;
mPhoneNumberEdit = (EditText) findViewById(R.id.phonenumber);
mContentEdit = (EditText) findViewById(R.id.content);
mSendBtn = (Button) findViewById(R.id.send);
mSendBtn.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
String phone = mPhoneNumberEdit.getText().toString().trim();
String content = mContentEdit.getText().toString().trim();
if (TextUtils.isEmpty(phone) && TextUtils.isEmpty(content)) {
Toast.makeText(mContext, "input message!", Toast.LENGTH_LONG).show();
return;
}
sendSMS(phone,content);
}

});

              //注册短信发送状态的广播

mReceiver = new SMSSendResultReceiver();
IntentFilter filter = new IntentFilter(SENT_SMS_ACTION);
registerReceiver(mReceiver, filter);
}

/** 
* 直接调用短信接口发短信    如果群发可以循环调用
* @param phoneNumber 
* @param message 
*/  
public void sendSMS(String phoneNumber,String message){  
//获取短信管理器   
android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();  
//拆分短信内容(手机短信长度限制)    
List divideContents = smsManager.divideMessage(message);
Intent itSend = new Intent(SENT_SMS_ACTION);  
itSend.putExtra(KEY_PHONENUM, phoneNumber);  
PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0, itSend, PendingIntent.FLAG_UPDATE_CURRENT);
for (String text : divideContents) {    
smsManager.sendTextMessage(phoneNumber, null, text, sentPI, null);    
}  

}


SMSSendResultReceiver mReceiver;


       // 监听短信发送状态

class SMSSendResultReceiver extends BroadcastReceiver  
{  
@Override  
public void onReceive(Context context, Intent intent)  
{  
if (SENT_SMS_ACTION.equals(intent.getAction())) {
String phoneNum = intent.getStringExtra(KEY_PHONENUM);  
switch(getResultCode())  
{  

case Activity.RESULT_OK: 

                                       // 发送成功

Toast.makeText(context, "Send Message to "+phoneNum+" success!", Toast.LENGTH_LONG).show();  
break;  
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:  
case SmsManager.RESULT_ERROR_RADIO_OFF:  
case SmsManager.RESULT_ERROR_NULL_PDU:  

default:  

                                        // 发送失败

Toast.makeText(context, "Send Message to "+phoneNum+" fail!", Toast.LENGTH_LONG).show();    
break;  
}  
}
}
}


@Override
protected void onDestroy() {
super.onDestroy();
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
}

}

sendTextMessage的参数说明:

public void sendTextMessage(String destinationAddress, 

String scAddress, String text,PendingIntent sentIntent, PendingIntent deliveryIntent)

destinationAddress: 收件人地址 

scAddress: 短信中心号码,null为默认中心号码

sentIntent: 当消息发出时,成功或者失败的信息报告通过PendingIntent来广播。如果该参数为空,则发信程序会被所有位置程序检查一遍,这样会导致发送时间延长。 

deliveryIntent: 当消息发送到收件人时,该PendingIntent会被广播。pdu数据在状态报告的extended data ("pdu")中。


你可能感兴趣的:(android)