android監聽簡訊

來源:互聯網
上載者:User

android中可以通過設定寫一個繼承自BroadcastReceiver的Receiver來重寫接收到簡訊之後的邏輯處理:

<receiver android:name=".filter.SmsReceiver"> 
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver> 

 

 

傳送簡訊後的Broadcast:

1 ContentObserver

先寫一個觀察者:

 

代碼

public class SmsObserver extends ContentObserver {        
        private Context context;
        private static final String[] SMS_PROJECTION = new String[] {
               "address","person","date","type","body",
            };

        public SmsObserver(Context context,Handler handler) {
                super(handler);                
                this.context = context;
                Log.i("Leo-SmsObserver", "My Oberver on create");
        }
        
        public void onChange (boolean selfChange) {
                Log.i("SmsObserver", "sms onChange###### ");                
        }

}

 

然後在Acitivty或Service裡註冊這個觀察者

 

getContentResolver().registerContentObserver(Uri.parse("content://sms"),
                                true, new SmsObserver(this, new Handler()));

 

 

2 可以自訂BroadcastReceiver來實現~

 

代碼

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
      
      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_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();
      }
    }
  }

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.