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