Android--判断发送短信后的状态/发送成功Or发送失败

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

相信用过诺基亚手机的朋友可能会记得,在短信发送之后,手机会显示发送手机的状态,Android同样也提供了相应的功能,而且实现这个功能的方法也很简单。

首先,我们需要在程序中注册两个权限:

1.接收短信的权限

2.发送短信的权限

  <uses-permission android:name="android.permission.SEND_SMS">uses-permission<uses-permission android:name="android.permission.RECEIVE_SMS">uses-permission>

之后,我们需要开启两个模拟器测试程序的功能,下面是实现的截图,首先看第一张截图,是一个模拟器接收到另一个模拟器发送来的短信显示在通知栏里:


我们会看到这个模拟器的端口号叫做5554,这个就相当于手机号,我们发送短信只要把这个号码当做是手机号使用即可,下面这张截图是主程序的实现截图,点击发送按钮,短信被发出,同时,如果成功,系统会发送广播信息。

截图如下:


下面给出程序的实现代码:

public class EX06_14 extends Activity/* 建立两个mServiceReceiver对象,作为类成员变量 */  private mServiceReceiver mReceiver01, mReceiver02;  private Button mButton1;  private TextView mTextView01;  private EditText mEditText1, mEditText2;    /* 自定义ACTION常数,作为广播的Intent Filter识别常数 */  private static String SMS_SEND_ACTIOIN = "SMS_SEND_ACTIOIN"private static String SMS_DELIVERED_ACTION = "SMS_DELIVERED_ACTION";      /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);        mTextView01 = (TextView)findViewById(R.id.myTextView1);        /* 电话号码 */    mEditText1 = (EditText) findViewById(R.id.myEditText1);        /* 短信内容 */    mEditText2 = (EditText) findViewById(R.id.myEditText2);        mButton1 = (Button) findViewById(R.id.myButton1);        //mEditText1.setText("+886935123456");    /* 设定预设为5556表示第二个模拟器的Port */    mEditText1.setText("5556");    mEditText2.setText("Hello DavidLanz!");        /* 发送SMS短信按钮事件处理 */    mButton1.setOnClickListener(new Button.OnClickListener()    {      @Override      public void onClick(View arg0)      {        // TODO Auto-generated method stub                /* 欲发送的电话号码 */        String strDestAddress = mEditText1.getText().toString();                /* 欲发送的短信内容 */        String strMessage = mEditText2.getText().toString();                /* 建立SmsManager对象 */        SmsManager smsManager = SmsManager.getDefault();                // TODO Auto-generated method stub        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);          mTextView01.setText(R.string.str_sms_sending);        }        catch(Exception e)        {          mTextView01.setText(e.toString());          e.printStackTrace();        }      }    });  }    /* 自定义mServiceReceiver重写BroadcastReceiver监听短信状态信息 */  public class mServiceReceiver extends BroadcastReceiver  {    @Override    public void onReceive(Context context, Intent intent)    {      // TODO Auto-generated method stub            //mTextView01.setText(intent.getAction().toString());      if (intent.getAction().equals(SMS_SEND_ACTIOIN))      {        try        {          /* android.content.BroadcastReceiver.getResultCode()方法 */          //Retrieve the current result code, as set by the previous receiver.          switch(getResultCode())          {            case Activity.RESULT_OK:              /* 发送短信成功 */              //mTextView01.setText(R.string.str_sms_sent_success);              mMakeTextToast              (                getResources().getText(R.string.str_sms_sent_success).toString(),                true              );              break;            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:              /* 发送短信失败 */              //mTextView01.setText(R.string.str_sms_sent_failed);              mMakeTextToast              (                getResources().getText(R.string.str_sms_sent_failed).toString(),                true              );              break;            case SmsManager.RESULT_ERROR_RADIO_OFF:              break;            case SmsManager.RESULT_ERROR_NULL_PDU:              break;          }                }        catch(Exception e)        {          mTextView01.setText(e.toString());          e.getStackTrace();        }      }      else if(intent.getAction().equals(SMS_DELIVERED_ACTION))      {        try        {          /* android.content.BroadcastReceiver.getResultCode()方法 */          switch(getResultCode())          {            case Activity.RESULT_OK:              /* 短信 */              //mTextView01.setText(R.string.str_sms_sent_success);              mMakeTextToast              (                getResources().getText(R.string.str_sms_rec_success).toString(),                true              );              break;            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:              /* 短信未送达 */              //mTextView01.setText(R.string.str_sms_sent_failed);              mMakeTextToast              (                getResources().getText(R.string.str_sms_rec_failed).toString(),                true              );              break;            case SmsManager.RESULT_ERROR_RADIO_OFF:              break;            case SmsManager.RESULT_ERROR_NULL_PDU:              break;          }                }        catch(Exception e)        {          mTextView01.setText(e.toString());          e.getStackTrace();        }      }          }  }    public void mMakeTextToast(String str, boolean isLong)  {    if(isLong==true)    {      Toast.makeText(EX06_14.this, str, Toast.LENGTH_LONG).show();    }    else    {      Toast.makeText(EX06_14.this, str, Toast.LENGTH_SHORT).show();    }  }  //这是重载Activity中的函数  @Override  protected void onResume()  {    // TODO Auto-generated method stub        /* 自定义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();  }    @Override  protected void onPause()  {    // TODO Auto-generated method stub        /* 取消注册自定义Receiver */    unregisterReceiver(mReceiver01);    unregisterReceiver(mReceiver02);        super.onPause();  }}


           

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

这里写图片描述

你可能感兴趣的:(Android--判断发送短信后的状态/发送成功Or发送失败)