发送并监听短信状态

来源:互联网 发布:游戏动画软件 编辑:程序博客网 时间:2024/04/30 00:55

这是从其它网站引过来的一段代码,主要是用来监听短信接收和发送情况

package lab.sodino.smslistener;

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.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SmsAct extends Activity {
    private TextView textView;
    private static String ACTION_SMS_SEND = "lab.sodino.sms.send";
    private static String ACTION_SMS_DELIVERY = "lab.sodino.sms.delivery";
    private SMSReceiver sendReceiver;
    private SMSReceiver deliveryReceiver;

    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:
                    textView.append("\n[Send]SMS Send:Successed!");
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    textView
                            .append("\n[Send]SMS Send:RESULT_ERROR_GENERIC_FAILURE!");
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    textView
                            .append("\n[Send]SMS Send:RESULT_ERROR_NO_SERVICE!");
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    textView.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:
                    textView.append("\n[Delivery]SMS Delivery:Success!");
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    textView
                            .append("\n[Delivery]SMS Delivery:RESULT_ERROR_GENERIC_FAILURE!");
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    textView
                            .append("\n[Delivery]SMS Delivery:RESULT_ERROR_NO_SERVICE!");
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    textView
                            .append("\n[Delivery]SMS Delivery:RESULT_ERROR_NULL_PDU!");
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    textView
                            .append("\n[Delivery]SMS Delivery:RESULT_ERROR_RADIO_OFF!");
                    break;
                }
            }
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button btn = new Button(this);
        btn.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                sendSMS();
            }
        });
        textView = new TextView(this);
        textView.setBackgroundColor(0xffffffff);
        textView.setTextColor(0xff0000ff);
        textView.setText("SMS processing...");
        btn.setText("发送短信");
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

        LinearLayout linear = new LinearLayout(this);
        linear.setOrientation(LinearLayout.VERTICAL);
        linear.setLayoutParams(textParams);
        linear.addView(btn);
        linear.addView(textView);
        setContentView(linear);
        sendReceiver = new SMSReceiver();
        IntentFilter sendFilter = new IntentFilter(ACTION_SMS_SEND);
        registerReceiver(sendReceiver, sendFilter);
        deliveryReceiver = new SMSReceiver();
        IntentFilter deliveryFilter = new IntentFilter(ACTION_SMS_DELIVERY);
        registerReceiver(deliveryReceiver, deliveryFilter);
    }

    private void sendSMS() {
        String smsAddress = "10086";
        String smsBody = "bylcx";
        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);
    }

    protected void onPaused() {
        unregisterReceiver(sendReceiver);
        unregisterReceiver(deliveryReceiver);
    }
}

听别人说联通的没有deliverly事件,移动的有

 

和 http://blog.csdn.net/actual_/article/details/6653637 可完成群发,并监听每条短信发送状态