1. 程式人生 > >Android--判斷髮送簡訊後的狀態/傳送成功Or傳送失敗

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

這裡寫圖片描述