添加权限
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission> <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
public class SmsSendState extends Activity {
private mServiceReceiver mReceiver01, mReceiver02;
private Button mButton1;
private TextView mTextView01;
private EditText mEditText1, mEditText2;
private static String SMS_SEND_ACTIOIN = "SMS_SEND_ACTIOIN";
private static String SMS_DELIVERED_ACTION = "SMS_DELIVERED_ACTION";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_send_state);
mTextView01 = (TextView) findViewById(R.id.myTextView1);
/* 电话号码 */
mEditText1 = (EditText) findViewById(R.id.myEditText1);
/* 短信内容 */
mEditText2 = (EditText) findViewById(R.id.myEditText2);
mButton1 = (Button) findViewById(R.id.myButton1);
/* 发送SMS短信按钮事件处理 */
mButton1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
/* 欲发送的电话号码 */
String strDestAddress = mEditText1.getText().toString();
/* 欲发送的短信内容 */
String strMessage = mEditText2.getText().toString();
/* 建立SmsManager对象 */
SmsManager smsManager = SmsManager.getDefault();
try {
/* 建立自定义Action常数的Intent(给PendingIntent参数之用) */
Intent itSend = new Intent(SMS_SEND_ACTIOIN);
Intent itDeliver = new Intent(SMS_DELIVERED_ACTION);
/* sentIntent参数为传送后接受的广播信息PendingIntent */
PendingIntent mSendPI = PendingIntent.getBroadcast(
getApplicationContext(), 0, itSend, 0);
/* deliveryIntent参数为送达后接受的广播信息PendingIntent */
PendingIntent mDeliverPI = PendingIntent.getBroadcast(
getApplicationContext(), 0, itDeliver, 0);
/* 发送SMS短信,注意倒数的两个PendingIntent参数 */
smsManager.sendTextMessage(strDestAddress, null,
strMessage, mSendPI, mDeliverPI);
} catch (Exception e) {
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
});
}
/* 自定义mServiceReceiver重写BroadcastReceiver监听短信状态信息 */
public class mServiceReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_SEND_ACTIOIN)) {
try {
switch (getResultCode()) {
case Activity.RESULT_OK:
/* 发送短信成功 */
mMakeTextToast("发送短信成功", true);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
/* 发送短信失败 */
mMakeTextToast("发送短信失败", true);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
}
} catch (Exception e) {
e.getStackTrace();
}
} else if (intent.getAction().equals(SMS_DELIVERED_ACTION)) {
try {
switch (getResultCode()) {
case Activity.RESULT_OK:
/* 短信 */
mMakeTextToast("发送短信成功", true);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
/* 短信未送达 */
mMakeTextToast("发送短信失败", true);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
}
} catch (Exception e) {
e.getStackTrace();
}
}
}
}
public void mMakeTextToast(String str, boolean isLong) {
if (isLong == true) {
Toast.makeText(SmsSendState.this, str, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SmsSendState.this, str, Toast.LENGTH_SHORT).show();
}
}
protected void onResume() {
/* 自定义IntentFilter为SENT_SMS_ACTIOIN Receiver */
IntentFilter mFilter01;
mFilter01 = new IntentFilter(SMS_SEND_ACTIOIN);
mReceiver01 = new mServiceReceiver();
registerReceiver(mReceiver01, mFilter01);
/* 自定义IntentFilter为DELIVERED_SMS_ACTION Receiver */
mFilter01 = new IntentFilter(SMS_DELIVERED_ACTION);
mReceiver02 = new mServiceReceiver();
registerReceiver(mReceiver02, mFilter01);
super.onResume();
}
protected void onPause() {
/* 取消注册自定义Receiver */
unregisterReceiver(mReceiver01);
unregisterReceiver(mReceiver02);
super.onPause();
}
}