http://blog.csdn.net/sodino/article/details/5871537
监听SMS发送状态的例子网上虽然有,但还是太杂了不完全。自己写了个。
短信服务中心号码的获取是通过SmsMessage.getServiceCenterAddress()方法获得。也就是只能从已经存储在你手机上的短信中获取。本例子获取短信服务号码的思路是发送一个自定义字符串到10086去,拦截10086下行的短信从中读取短信服务号码。测试后发现虽然在本手机卡为深圳号,但收到的短信服务中心号码却是佛山的。但至少知道是处于哪个省了。
全国短信服务中心号码详见:http://zdm2008.blog.163.com/blog/static/20491545201062210921272/
另外一份详细的中国联通的短信服务中心号码详见:http://wenku.baidu.com/view/7acddc1b6bd97f192279e9c0.html
本例子还用Java代码实现布局。
send:短信是否发送出去。
delivery:短信是否发送到收件人中。
- package lab.sodino.servicecenteraddress;
- 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.os.Bundle;
- import android.telephony.gsm.SmsManager;
- import android.telephony.gsm.SmsMessage;
- import android.view.Gravity;
- import android.view.View;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import android.widget.LinearLayout.LayoutParams;
- public class ServiceCenterAddressAct extends Activity {
- private static final String ACTION_SMS_SEND = "lab.sodino.sms.send";
- private static final String ACTION_SMS_DELIVERY = "lab.sodino.sms.delivery";
- private static final String ACTION_SMS_RECEIVER = "android.provider.Telephony.SMS_RECEIVED";
- private TextView serviceCenterAddressText;
- private SMSReceiver sendReceiver;
- private SMSReceiver deliveryReceiver;
- private SMSReceiver smsReceiver;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- LinearLayout.LayoutParams layParams = new LinearLayout.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
- LinearLayout linearLay = new LinearLayout(this);
- linearLay.setOrientation(LinearLayout.VERTICAL);
- linearLay.setLayoutParams(layParams);
- TextView textView = new TextView(this);
- textView.setBackgroundColor(0xffffffff);
- textView.setTextColor(0xff0000ff);
- textView.setTextSize(20);
- textView.setText("点击发送按钮将发送自定义字符串至10086");
- textView.setGravity(Gravity.CENTER);
- linearLay.addView(textView);
- Button btnSend = new Button(this);
- // LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(
- // LinearLayout.LayoutParams.FILL_PARENT,
- // LinearLayout.LayoutParams.WRAP_CONTENT);
- btnSend.setText("发送");
- btnSend.setOnClickListener(new Button.OnClickListener() {
- public void onClick(View v) {
- serviceCenterAddressText.setText("正在等待发送短信...");
- sendSms();
- }
- });
- linearLay.addView(btnSend);
- serviceCenterAddressText = new TextView(this);
- serviceCenterAddressText.setText("正在等待发送短信...");
- serviceCenterAddressText.setBackgroundColor(0xffffffff);
- serviceCenterAddressText.setTextColor(0xff0000ff);
- serviceCenterAddressText.setTextSize(20);
- serviceCenterAddressText.setGravity(Gravity.LEFT);
- linearLay.addView(serviceCenterAddressText);
- setContentView(linearLay);
- // 注册send
- sendReceiver = new SMSReceiver();
- IntentFilter sendFilter = new IntentFilter(ACTION_SMS_SEND);
- registerReceiver(sendReceiver, sendFilter);
- // 注册delivery
- deliveryReceiver = new SMSReceiver();
- IntentFilter deliveryFilter = new IntentFilter(ACTION_SMS_DELIVERY);
- registerReceiver(deliveryReceiver, deliveryFilter);
- // 注册接收下行receiver
- smsReceiver = new SMSReceiver();
- IntentFilter receiverFilter = new IntentFilter(ACTION_SMS_RECEIVER);
- registerReceiver(smsReceiver, receiverFilter);
- }
- protected void onPause() {
- unregisterReceiver(sendReceiver);
- unregisterReceiver(deliveryReceiver);
- unregisterReceiver(smsReceiver);
- }
- private void sendSms() {
- String smsBody = "lab.sodino.sms.test";
- String smsAddress = "10086";
- SmsManager smsMag = SmsManager.getDefault();
- Intent sendIntent = new Intent(ACTION_SMS_SEND);
- PendingIntent sendPI = PendingIntent.getBroadcast(this, 0, sendIntent,
- 0);
- Intent deliveryIntent = new Intent(ACTION_SMS_DELIVERY);
- PendingIntent deliveryPI = PendingIntent.getBroadcast(this, 0,
- deliveryIntent, 0);
- smsMag.sendTextMessage(smsAddress, null, smsBody, sendPI, deliveryPI);
- }
- public class SMSReceiver extends BroadcastReceiver {
- public void onReceive(Context context, Intent intent) {
- String actionName = intent.getAction();
- int resultCode = getResultCode();
- if (actionName.equals(ACTION_SMS_SEND)) {
- switch (resultCode) {
- case Activity.RESULT_OK:
- serviceCenterAddressText
- .append("/n[Send]SMS Send:Successed!");
- break;
- case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
- serviceCenterAddressText
- .append("/n[Send]SMS Send:RESULT_ERROR_GENERIC_FAILURE!");
- break;
- case SmsManager.RESULT_ERROR_NO_SERVICE:
- serviceCenterAddressText
- .append("/n[Send]SMS Send:RESULT_ERROR_NO_SERVICE!");
- break;
- case SmsManager.RESULT_ERROR_NULL_PDU:
- serviceCenterAddressText
- .append("/n[Send]SMS Send:RESULT_ERROR_NULL_PDU!");
- break;
- case SmsManager.RESULT_ERROR_RADIO_OFF:
- break;
- }
- } else if (actionName.equals(ACTION_SMS_DELIVERY)) {
- switch (resultCode) {
- case Activity.RESULT_OK:
- serviceCenterAddressText
- .append("/n[Delivery]SMS Delivery:Successed!");
- break;
- case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
- serviceCenterAddressText
- .append("/n[Delivery]SMS Delivery:RESULT_ERROR_GENERIC_FAILURE!");
- break;
- case SmsManager.RESULT_ERROR_NO_SERVICE:
- serviceCenterAddressText
- .append("/n[Delivery]SMS Delivery:RESULT_ERROR_NO_SERVICE!");
- break;
- case SmsManager.RESULT_ERROR_NULL_PDU:
- serviceCenterAddressText
- .append("/n[Delivery]SMS Delivery:RESULT_ERROR_NULL_PDU!");
- break;
- case SmsManager.RESULT_ERROR_RADIO_OFF:
- serviceCenterAddressText
- .append("/n[Delivery]SMS Delivery:RESULT_ERROR_RADIO_OFF!");
- break;
- }
- serviceCenterAddressText.append("/n正在等待下行短信...");
- } else if (actionName.equals(ACTION_SMS_RECEIVER)) {
- System.out.println("[Sodino]result = " + resultCode);
- Bundle bundle = intent.getExtras();
- if (bundle != null) {
- Object[] myOBJpdus = (Object[]) bundle.get("pdus");
- SmsMessage[] messages = new SmsMessage[myOBJpdus.length];
- for (int i = 0; i < myOBJpdus.length; i++) {
- messages[i] = SmsMessage
- .createFromPdu((byte[]) myOBJpdus[i]);
- }
- SmsMessage message = messages[0];
- serviceCenterAddressText.append("/n短信服务中心号码为:"
- + message.getServiceCenterAddress());
- }
- }
- }
- }
- }
最后要在AndroidManifest.xml中添加下面两个权限:
- <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
- <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>