问题
I am working on android project where I need to send SMS. My app collects required information by consuming a web service and this information is very short and pure text. This information is then sent in the form of SMS.
I have used broadcast receiver that will keep track of whether SMS is sent successfully or not and simply add a log entry. I have used SmsManager to send SMS.
My device is having very good WiFi strength and good GPRS network. While sending SMS, I have found that broadcast receiver inserts log entries, some for "Successful" and some for "Generic Failure".
Why few SMS fail because of "Generic Failure"? What is the reason behind this?
I have googled and found that some people are saying to turn OFF WiFi. But for consuming web service I need WiFi ON.
Can anyone give some insight on this? Is there any solution for this problem?
回答1:
If you are sending a lot sms together it will flood your phone so it is preferable to have some delay.
Next if you do delay you have to see to that it is not done in the UI thread or else you will get ANR.
Try using handlers, my friend suggested this, I tried and it works fine.
As for Generic issue, I am not sure.. The name Generic makes it sound like it could be a normal network error.
Hope this information is useful.
回答2:
I had already to overcome this generic failure message with the help of time delay to send one device to multiple numbers, It almost remove the generic failure
for(int index=0; index < phone.length; index++){
phonenumber=phone[index];
Toast.makeText(cxt, "Phone number is: "+phonenumber, Toast.LENGTH_LONG).show();
if(index==0){
Send_SMS(phonenumber.toString().trim(), textmessage);
}
else{
new Handler().postDelayed(new Runnable() {
public void run() {
Send_SMS(phonenumber.toString().trim(), textmessage);
}
}, 1000*40);
}
}
public void Send_SMS(String phonenumber, String message){
// here you use sms manager to send the sms
}
回答3:
add this permissions in your AndroidManifest file
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
回答4:
I have met similar problem. After couple of minutes, I found the phone number I try to send is invalid.
Thus, any one has this problem, please check the phone number first!
回答5:
I had the same problem and found out I was out of my credit balance in my mobile.
回答6:
Try this:
String [] cellArray = phNumbers.getText().toString().split(";");
mMessageSentCount = 0;
String cellno = cellArray[mMessageSentCount].toString().trim();
startSendMessages(cellno);
private void startSendMessages(String ph){
registerBroadCastReceivers();
sendSMS(ph, mBody);
}
private void sendNextMessage(int mMessageSentCount){
String ph = cellArray[mMessageSentCount].toString().trim();
sendSMS(ph, mBody);
}
private boolean thereAreSmsToSend(){
return mMessageSentCount < cellArray.length;
}
private void sendSMS(final String phoneNumber, String message) {
Toast.makeText(getBaseContext(), "Phone number is: "+phoneNumber, Toast.LENGTH_SHORT).show();
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> parts = sms.divideMessage(message);
mMessageSentTotalParts = parts.size();
Log.i("Message Count", "Message Count: " + mMessageSentTotalParts);
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 < mMessageSentTotalParts; j++) {
sentIntents.add(sentPI);
deliveryIntents.add(deliveredPI);
}
mMessageSentParts = 0;
sms.sendMultipartTextMessage(phoneNumber, null, parts, sentIntents, deliveryIntents);
}
private void registerBroadCastReceivers(){
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
mMessageSentParts++;
if ( mMessageSentParts == mMessageSentTotalParts ) {
mMessageSentCount++;
if(thereAreSmsToSend()){
sendNextMessage(mMessageSentCount);
} else{
Toast.makeText(getBaseContext(), "All SMS have been sent",Toast.LENGTH_SHORT).show();
}
}
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));
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));
}
回答7:
I found that once the data is over 160 chars, i got a generic failure.
回答8:
I had the same problem and I solved it by removing the sim phone from sendTextMessage
and made it null
来源:https://stackoverflow.com/questions/9769095/why-do-i-get-generic-failure-error-while-sending-sms