首先,定義兩個廣播接收者BroadcastReceiver
/**?發送與接收的廣播?**/String?SENT_SMS_ACTION?=?"SENT_SMS_ACTION";
String?DELIVERED_SMS_ACTION?=?"DELIVERED_SMS_ACTION";
//負責接收短信發送的情況廣播
private?BroadcastReceiver?sendMessage?=?new?BroadcastReceiver()?{
@Override
public?void?onReceive(Context?context,?Intent?intent)?{
//?判斷短信是否發送成功
switch?(getResultCode())?{
case?Activity.RESULT_OK: //短信發送成功
default:
break;
}
}
};
//負責接收對方成功收到短信的廣播
private?BroadcastReceiver?receiver?=?new?BroadcastReceiver()?{
@Override
public?void?onReceive(Context?context,?Intent?intent)?{
//?表示對方成功收到短信
}
};
然後,不要忘了在初始化方法中註冊壹下上面定義的兩個廣播接收者,註冊代碼為:
registerReceiver(sendMessage,?new?IntentFilter(SENT_SMS_ACTION));
registerReceiver(receiver,?new?IntentFilter(DELIVERED_SMS_ACTION));