我制作短信应用程序,如果消息长度< 160,但是,当消息长度超过160时,仿真器表示应用程序强制关闭, 我怎么解决这个问题? 这是我的代码..
private void kirimSMS(String string1, String string2) {
pi1 = PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
pi2 = PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"),
0);
sendBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(KirimSMS.this.getBaseContext(),
"SMS terkirim", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(KirimSMS.this.getBaseContext(),
"Gagal Kirim", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(KirimSMS.this.getBaseContext(),
"No Services", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(KirimSMS.this.getBaseContext(), "No PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(KirimSMS.this.getBaseContext(), "Radio Off",
Toast.LENGTH_SHORT).show();
break;
}
}};
deliveryBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(KirimSMS.this.getBaseContext(),
"Pesan terkirim", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(KirimSMS.this.getBaseContext(),
"Pesan Tidak terkirim", Toast.LENGTH_SHORT).show();
break;
}
}
};
registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
//SmsManager.getDefault().sendTextMessage(string1, null, string2, pi1,pi2);
SmsManager smsManager = SmsManager.getDefault();
ArrayList<String> parts = smsManager.divideMessage(stringIsiPesan);
smsManager.sendMultipartTextMessage(stringNoHp, null, parts, null, null);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btnEnkripsi:
if (cekField()) {
String kunci = null;
String hasil = null;
if (MainActivity.DEFAULT_KEY.equals(""))
kunci = "ABCD";
else
kunci = MainActivity.DEFAULT_KEY;
Kriptoku enktripsi = new Kriptoku();
hasil = enktripsi.enkripsibaru(txtPesan.getText().toString(),kunci);
txtPesan.setText(hasil);
btnEnkripsi.setEnabled(false);
} else {
AlertDialog.Builder loBuilder2 = new AlertDialog.Builder(this);loBuilder2
.setMessage("lengkapi data..")
.setTitle("Oo..Oo..")
.setPositiveButton("OK",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
}
});
loBuilder2.create();
loBuilder2.show();
}
break;
case R.id.btnKirimPesan:
//if (txtPesan.length() <= 160) {
if (cekField()) {
kirimSMS(this.txtNoHp.getText().toString(), this.txtPesan.getText().toString());
dao = SQLiteDAO.getInstance(this,
new Class[] { Outbox.class });
Outbox localSMS = new Outbox();
localSMS.setNoHp(this.txtNoHp.getText().toString());
localSMS.setPesan(this.txtPesan.getText().toString());
SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("HH:mm, dd MMM yyyy");
Calendar localCalendar = Calendar.getInstance();
localSMS.setTime(localSimpleDateFormat.format(localCalendar.getTime()));
dao.insert(localSMS);
finish();
} else {
AlertDialog.Builder loBuilder1 = new AlertDialog.Builder(this);
loBuilder1
.setMessage("Lengkapi Data")
.setTitle("Aduuhh...")
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick{ DialogInterface dialog,int which) {
//TODO Auto-generated method stub
}
});
loBuilder1.create();
loBuilder1.show();
}
} //else {
// Toast.makeText(this, String.valueOf("Karakter lebih dari 160"), Toast.LENGTH_LONG).show();
//return;
// break;
}
拜托,帮帮我
答案 0 :(得分:1)
SMS = 短消息服务,其中'short'为160个字符或更少。如果您想发送更多内容,请将邮件分成两个或更多部分。
答案 1 :(得分:1)
默认情况下,SMS的最大长度只能为160个字符
SHORT 在SMS中的含义=“短消息服务”
如果您仍想发送超过160个字符,则需要将其拆分为两个或将其转换为彩信
所以使用if
条件检查长度是否大于160个字符