嘿,我正在创建需要发送 SMS 的 android 项目,这是我的代码:
private void sendSMS(String destSMS, String msgSMS) {
// TODO Auto-generated method stub
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent pi = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveri_pi = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
BroadcastReceiver brSend = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS Delivered",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic Failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No Service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio Off",
Toast.LENGTH_SHORT).show();
break;
}
}
};
BroadcastReceiver brDelivered = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(brSend, new IntentFilter(SENT));
registerReceiver(brDelivered, new IntentFilter(DELIVERED));
SmsManager smsman = SmsManager.getDefault();
smsman.sendTextMessage(destSMS, null, msgSMS, pi, deliveri_pi);
}
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String dest, messageEnc;
dest = etDest.getText().toString();
messageEnc = etCipher.getText().toString();
try {
sendSMS(dest, messageEnc);
} catch (Exception e) {
Toast.makeText(getBaseContext(),
"Gagal karena " + e.toString(), Toast.LENGTH_SHORT)
.show();
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
问题在于文本字段文本是 = 'th!Azasd" 随机文本,但是当我尝试发送到另一个模拟器时收到的消息不同,消息接收就像“@hu@@@”完全不同,消息接收长度很多更多并填写一些unicode符号..对不起我的英语。