1
我知道有一些明顯的錯誤,但我試過很多東西並不能得到這種方法來讀取全局字符串:sendSMS()方法無法讀取字符串
public void sendSMS(String phoneNumber) {
SmsManager smsManager = SmsManager.getDefault();
String message = mMessage;
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
int messageCount = parts.size();
Log.i("Message Count", "Message Count: " + messageCount);
Log.i("Order", message);
Toast.makeText(getBaseContext(), message,
Toast.LENGTH_SHORT).show();
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>();
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
new Intent(DELIVERED), 0);
for (int j = 0; j < messageCount; j++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
// ---when the SMS has been sent---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), sms_sent,
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;
}
}
}, new IntentFilter(SENT));
// ---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
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;
}
}
}, new IntentFilter(DELIVERED));
smsManager.sendTextMessage(phoneNumber, null, message, sentPI,
deliveredPI);
/*
* sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents,
* deliveryIntents);
*/
}
這種方法在保存ViewPager片段的MainActivity。字符串被收集,然後連接起來的片段用下面的方法:
public void getListToString(){
StringBuilder sb = new StringBuilder();
for(ReviewItem item : mCurrentReviewItems){
sb.append(item.getTitle() + ": " + item.getDisplayValue() + "\n");
}
String msg = sb.toString();
((MainActivity)getActivity()).setMessage(msg);
}
可以肯定 - 我做了記錄與mMessage和祝酒詞。每個項目都顯示,但sendSMS()不想發送它。雖然,當在String消息而不是mMessage時,我把「一些字符串在這裏」,它完美的作品。任何人都可以捕捉到讓sendTextMessage無法訪問的公共String mMessage?
我注意到你已經註釋掉了'sendMultipartTextMessage()'方法。你確定你沒有達到'sendTextMessage()'的最大字符數限制嗎? – 2014-10-07 00:05:14
它沒有它的其他應用程序工作,所以可能並非如此。無論如何,我使用的是Moto G,因此很難達到極限;) – 2014-10-07 11:23:51
我不認爲你明白字符限制的含義。 SMS協議爲單個消息定義了160個字符的限制(對於純ASCII)。這個數字可能會更低,這取決於使用的字母表。如果總消息比這更長,則需要將其拆分並使用多部分方法發送。這與設備型號沒有任何關係。我建議你使用多部分方法進行測試,因爲如果消息太長,'sendTextMessage()'就會失敗。 – 2014-10-07 11:43:19