3.1、運行SMS程序給另一個android模擬器發短信
運行上面我們編寫的TextMessage程序,另外在Windows的命令行下切換到tools目錄下,并輸入emulator –data smsReceiver,我的如下:
這樣就會啟動一個android模擬器,如下所示:( 注意它的編號:5556,就是用這個編號與它通信的 )
圖2、通過emulator啟動一個android模擬器
通過我們TextMessage程序啟動的android模擬器,編寫短信:
圖3、TextMessage程序個5556模擬器發短信
點擊發送之后,通過命令行啟動的5556號android模擬器會收到我們剛才發送的短信,如下所示:
圖4、收到短信的提示
tips:
如果通過命令行的emulator啟動android模擬器提示“NO DNS servers found!”,這時我們發的短信模擬器是收不到的。
-
在Windows下,如果電腦沒有介入網絡,即找不DNS服務器的話會出現這種情況!
-
在Mac下,如果提示這個警告的話,可以這樣解決:檢查你是否有
/etc/resolv.conf文件,如果沒有的話,通過下面的命令行ln -s /private/var/run/resolv.conf /etc/resolv.conf可以解決。
4、SMS增強(一)
上面我們實現了一個簡單的SMS程序,下面我們要對它進行增強!你肯定已經注意到了,我們上面的SMS程序的sendTextMessage方法中的第4個和第5個參數PendingIntent設為null,即sentIntent和deliveryIntent。
第4個參數-sendIntent,當消息成功發送或發送失敗都將被觸發。廣播接收者的結果碼,Activity.RESULT_OK表示成功,或RESULT_ERROR_GENERIC_FAILURE、RESULT_ERROR_RADIO_OFF、RESULT_ERROR_NULL_PDU之一表示錯誤。對應RESULT_ERROR_GENERIC_FAILURE, sentIntent 可能包括額外的“錯誤代碼”包含一個無線電廣播技術特定的值,通常只在修復故障時有用。第5個參數-deliveryIntent,僅當目標接收到你的SMS消息才觸發。
為了跟蹤發出的短信的狀態,實現和注冊Broadcast Receiver(廣播接收者)監聽傳遞給sendTextMessage方法的參數Pending Intents。下面我們就實現和注冊這個廣播接收者:
String SENT_SMS_ACTION="
SENT_SMS_ACTION
";
String DELIVERED_SMS_ACTION="
DELIVERED_SMS_ACTION
";
//create the sentIntent parameter
Intent sentIntent=
new
Intent(SENT_SMS_ACTION);
PendingIntent sentPI=PendingIntent.getBroadcast(
this
,
0,
sentIntent,
0);
//create the deilverIntent parameter
Intent deliverIntent=
new
Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI=PendingIntent.getBroadcast(
this
,
0,
deliverIntent,
0);
//register the Broadcast Receivers
registerReceiver(
new
BroadcastReceiver(){
@Override
public
void
onReceive(Context _context,Intent _intent)
{
switch
(getResultCode()){
case
Activity.RESULT_OK:
Toast.makeText(getBaseContext(),
"
SMS sent success actions
",
Toast.LENGTH_SHORT).show();
break
;
case
SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(),
"
SMS generic failure actions
",
Toast.LENGTH_SHORT).show();
break
;
case
SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(),
"
SMS radio off failure actions
",
Toast.LENGTH_SHORT).show();
break
;
case
SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),
"
SMS null PDU failure actions
",
Toast.LENGTH_SHORT).show();
break
;
}
}
},
new
IntentFilter(SENT_SMS_ACTION));
registerReceiver(
new
BroadcastReceiver(){
@Override
public
void
onReceive(Context _context,Intent _intent)
{
Toast.makeText(getBaseContext(),
"
SMS delivered actions
",
Toast.LENGTH_SHORT).show();
}
},
new
IntentFilter(DELIVERED_SMS_ACTION));
?
在基本完成了要做的工作,接下來要做的就是將sendTextMessage的第4個和第5個參數改為sentPI、deliverPI,這樣工作基本完成,修改后的sendSMS方法如下:
private
void
sendSMS(String phoneNumber, String message) {
// ---sends an SMS message to another device---
SmsManager sms = SmsManager.getDefault();
String SENT_SMS_ACTION = "
SENT_SMS_ACTION
";
String DELIVERED_SMS_ACTION = "
DELIVERED_SMS_ACTION
";
// create the sentIntent parameter
Intent sentIntent =
new
Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(
this
, 0, sentIntent,
0);
// create the deilverIntent parameter
Intent deliverIntent =
new
Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = PendingIntent.getBroadcast(
this
, 0,
deliverIntent, 0);
// register the Broadcast Receivers
registerReceiver(
new
BroadcastReceiver() {
@Override
public
void
onReceive(Context _context, Intent _intent) {
switch
(getResultCode()) {
case
Activity.RESULT_OK:
Toast.makeText(getBaseContext(),
"
SMS sent success actions
", Toast.LENGTH_SHORT)
.show();
break
;
case
SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(),
"
SMS generic failure actions
", Toast.LENGTH_SHORT)
.show();
break
;
case
SmsManager.RESULT_ERROR_RADIO_OFF:
Toast
.makeText(getBaseContext(),
"
SMS radio off failure actions
",
Toast.LENGTH_SHORT).show();
break
;
case
SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(),
"
SMS null PDU failure actions
", Toast.LENGTH_SHORT)
.show();
break
;
}
}
},
new
IntentFilter(SENT_SMS_ACTION));
registerReceiver(
new
BroadcastReceiver() {
@Override
public
void
onReceive(Context _context, Intent _intent) {
Toast.makeText(getBaseContext(), "
SMS delivered actions
",
Toast.LENGTH_SHORT).show();
}
},
new
IntentFilter(DELIVERED_SMS_ACTION));
// if message's length more than 70 ,
// then call divideMessage to dive message into several part ,and call
// sendTextMessage()
// else direct call sendTextMessage()
if
(message.length() > 70) {
ArrayList<String> msgs = sms.divideMessage(message);
for
(String msg : msgs) {
sms.sendTextMessage(phoneNumber,
null
, msg, sentPI, deliverPI);
}
}
else
{
sms.sendTextMessage(phoneNumber,
null
, message, sentPI, deliverPI);
}
}
?
運行之后的,發送短信成功的話就可以看到如下界面:
圖5、增強SMS(一)
5、SMS增強(二)
下面這個增強是使SMS能夠發送二進制數據。要發送數據要使用SmsManager類的sendDataMessage方法,跟sendTextMessage方法類似,只不過該方法多了一個目標端口的參數,構建該SMS的過程跟前面的類似這里就不在累述。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

