Android监听SMS发送状态并获取短信服务中心号码

时间:2021-11-22 08:55:31

Android监听SMS发送状态并获取短信服务中心号码2010-09-25 10:57

监听SMS发送状态的例子网上虽然有,但还是太杂了不完全。自己写了个。

短信服务中心号码的获取是通过SmsMessage.getServiceCenterAddress()方法获得。也就是只能从已经存储在你手机上的短信中获取。本例子获取短信服务号码的思路是发送一个自定义字符串到10086去,拦截10086下行的短信从中读取短信服务号码。测试后发现虽然在本手机卡为深圳号,但收到的短信服务中心号码却是佛山的。但至少知道是处于哪个省了。

全国短信服务中心号码详见:http://zdm2008.blog.163.com/blog/static/20491545201062210921272/

另外一份详细的中国联通的短信服务中心号码详见:http://wenku.baidu.com/view/7acddc1b6bd97f192279e9c0.html

本例子还用Java代码实现布局。

send:短信是否发送出去。

delivery:短信是否发送到收件人中。

view plaincopy to clipboardprint?
01.package lab.sodino.servicecenteraddress;   
02.import android.app.Activity;   
03.import android.app.PendingIntent;   
04.import android.content.BroadcastReceiver;   
05.import android.content.Context;   
06.import android.content.Intent;   
07.import android.content.IntentFilter;   
08.import android.os.Bundle;   
09.import android.telephony.gsm.SmsManager;   
10.import android.telephony.gsm.SmsMessage;   
11.import android.view.Gravity;   
12.import android.view.View;   
13.import android.widget.Button;   
14.import android.widget.LinearLayout;   
15.import android.widget.TextView;   
16.import android.widget.LinearLayout.LayoutParams;   
17.public class ServiceCenterAddressAct extends Activity {   
18.    private static final String ACTION_SMS_SEND = "lab.sodino.sms.send";   
19.    private static final String ACTION_SMS_DELIVERY = "lab.sodino.sms.delivery";   
20.    private static final String ACTION_SMS_RECEIVER = "android.provider.Telephony.SMS_RECEIVED";   
21.    private TextView serviceCenterAddressText;   
22.    private SMSReceiver sendReceiver;   
23.    private SMSReceiver deliveryReceiver;   
24.    private SMSReceiver smsReceiver;   
25.    /** Called when the activity is first created. */ 
26.    @Override 
27.    public void onCreate(Bundle savedInstanceState) {   
28.        super.onCreate(savedInstanceState);   
29.        LinearLayout.LayoutParams layParams = new LinearLayout.LayoutParams(   
30.                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);   
31.        LinearLayout linearLay = new LinearLayout(this);   
32.        linearLay.setOrientation(LinearLayout.VERTICAL);   
33.        linearLay.setLayoutParams(layParams);   
34.        TextView textView = new TextView(this);   
35.        textView.setBackgroundColor(0xffffffff);   
36.        textView.setTextColor(0xff0000ff);   
37.        textView.setTextSize(20);   
38.        textView.setText("点击发送按钮将发送自定义字符串至10086");   
39.        textView.setGravity(Gravity.CENTER);   
40.        linearLay.addView(textView);   
41.        Button btnSend = new Button(this);   
42.        // LinearLayout.LayoutParams btnParams = new LinearLayout.LayoutParams(   
43.        // LinearLayout.LayoutParams.FILL_PARENT,   
44.        // LinearLayout.LayoutParams.WRAP_CONTENT);   
45.        btnSend.setText("发送");   
46.        btnSend.setOnClickListener(new Button.OnClickListener() {   
47.            public void onClick(View v) {   
48.                serviceCenterAddressText.setText("正在等待发送短信...");   
49.                sendSms();   
50.            }   
51.        });   
52.        linearLay.addView(btnSend);   
53.        serviceCenterAddressText = new TextView(this);   
54.        serviceCenterAddressText.setText("正在等待发送短信...");   
55.        serviceCenterAddressText.setBackgroundColor(0xffffffff);   
56.        serviceCenterAddressText.setTextColor(0xff0000ff);   
57.        serviceCenterAddressText.setTextSize(20);   
58.        serviceCenterAddressText.setGravity(Gravity.LEFT);   
59.        linearLay.addView(serviceCenterAddressText);   
60.        setContentView(linearLay);   
61.        // 注册send   
62.        sendReceiver = new SMSReceiver();   
63.        IntentFilter sendFilter = new IntentFilter(ACTION_SMS_SEND);   
64.        registerReceiver(sendReceiver, sendFilter);   
65.        // 注册delivery   
66.        deliveryReceiver = new SMSReceiver();   
67.        IntentFilter deliveryFilter = new IntentFilter(ACTION_SMS_DELIVERY);   
68.        registerReceiver(deliveryReceiver, deliveryFilter);   
69.        // 注册接收下行receiver   
70.        smsReceiver = new SMSReceiver();   
71.        IntentFilter receiverFilter = new IntentFilter(ACTION_SMS_RECEIVER);   
72.        registerReceiver(smsReceiver, receiverFilter);   
73.    }   
74.    protected void onPause() {   
75.        unregisterReceiver(sendReceiver);   
76.        unregisterReceiver(deliveryReceiver);   
77.        unregisterReceiver(smsReceiver);   
78.    }   
79.    private void sendSms() {   
80.        String smsBody = "lab.sodino.sms.test";   
81.        String smsAddress = "10086";   
82.        SmsManager smsMag = SmsManager.getDefault();   
83.        Intent sendIntent = new Intent(ACTION_SMS_SEND);   
84.        PendingIntent sendPI = PendingIntent.getBroadcast(this, 0, sendIntent,   
85.                0);   
86.        Intent deliveryIntent = new Intent(ACTION_SMS_DELIVERY);   
87.        PendingIntent deliveryPI = PendingIntent.getBroadcast(this, 0,   
88.                deliveryIntent, 0);   
89.        smsMag.sendTextMessage(smsAddress, null, smsBody, sendPI, deliveryPI);   
90.    }   
91.    public class SMSReceiver extends BroadcastReceiver {   
92.        public void onReceive(Context context, Intent intent) {   
93.            String actionName = intent.getAction();   
94.            int resultCode = getResultCode();   
95.            if (actionName.equals(ACTION_SMS_SEND)) {   
96.                switch (resultCode) {   
97.                case Activity.RESULT_OK:   
98.                    serviceCenterAddressText   
99.                            .append("/n[Send]SMS Send:Successed!");   
100.                    break;   
101.                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:   
102.                    serviceCenterAddressText   
103.                            .append("/n[Send]SMS Send:RESULT_ERROR_GENERIC_FAILURE!");   
104.                    break;   
105.                case SmsManager.RESULT_ERROR_NO_SERVICE:   
106.                    serviceCenterAddressText   
107.                            .append("/n[Send]SMS Send:RESULT_ERROR_NO_SERVICE!");   
108.                    break;   
109.                case SmsManager.RESULT_ERROR_NULL_PDU:   
110.                    serviceCenterAddressText   
111.                            .append("/n[Send]SMS Send:RESULT_ERROR_NULL_PDU!");   
112.                    break;   
113.                case SmsManager.RESULT_ERROR_RADIO_OFF:   
114.                    break;   
115.                }   
116.            } else if (actionName.equals(ACTION_SMS_DELIVERY)) {   
117.                switch (resultCode) {   
118.                case Activity.RESULT_OK:   
119.                    serviceCenterAddressText   
120.                            .append("/n[Delivery]SMS Delivery:Successed!");   
121.                    break;   
122.                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:   
123.                    serviceCenterAddressText   
124.                            .append("/n[Delivery]SMS Delivery:RESULT_ERROR_GENERIC_FAILURE!");   
125.                    break;   
126.                case SmsManager.RESULT_ERROR_NO_SERVICE:   
127.                    serviceCenterAddressText   
128.                            .append("/n[Delivery]SMS Delivery:RESULT_ERROR_NO_SERVICE!");   
129.                    break;   
130.                case SmsManager.RESULT_ERROR_NULL_PDU:   
131.                    serviceCenterAddressText   
132.                            .append("/n[Delivery]SMS Delivery:RESULT_ERROR_NULL_PDU!");   
133.                    break;   
134.                case SmsManager.RESULT_ERROR_RADIO_OFF:   
135.                    serviceCenterAddressText   
136.                            .append("/n[Delivery]SMS Delivery:RESULT_ERROR_RADIO_OFF!");   
137.                    break;   
138.                }   
139.                serviceCenterAddressText.append("/n正在等待下行短信...");   
140.            } else if (actionName.equals(ACTION_SMS_RECEIVER)) {   
141.                System.out.println("[Sodino]result = " + resultCode);   
142.                Bundle bundle = intent.getExtras();   
143.                if (bundle != null) {   
144.                    Object[] myOBJpdus = (Object[]) bundle.get("pdus");   
145.                    SmsMessage[] messages = new SmsMessage[myOBJpdus.length];   
146.                    for (int i = 0; i < myOBJpdus.length; i++) {   
147.                        messages[i] = SmsMessage   
148.                                .createFromPdu((byte[]) myOBJpdus[i]);   
149.                    }   
150.                    SmsMessage message = messages[0];   
151.                    serviceCenterAddressText.append("/n短信服务中心号码为:" 
152.                            + message.getServiceCenterAddress());   
153.                }   
154.            }   
155.        }   
156.    }   
157.} 
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>

本文来自CSDN博客,转载自:http://blog.csdn.net/sodino/archive/2010/09/08/5871537.aspx