问题
I am using below code for sending sms in background for multiple peoples(Contacts) at a time. It is working for smaller text but however it not working for text which has more than 160 characters. I am getting GENERIC FAILURE.
public class SendSMS {
private int mMessageSentParts;
private int mMessageSentTotalParts;
private int mMessageSentCount;
private String message;
private String[] array;
private Context mContext;
public SendSMS(Context context,String array[]) {
// TODO Auto-generated constructor stub
this.mContext = context;
this.array = array;
System.out.println("array length :::: "+array.length);
message = mContext.getResources().getString(R.string.siri);
// message="siri";
startSendMessages();
}
private void startSendMessages() {
registerBroadCastReceivers();
mMessageSentCount = 0;
sendSMS(array[mMessageSentCount].toString(), message);
}
private void sendNextMessage() {
if (thereAreSmsToSend()) {
sendSMS(array[mMessageSentCount].toString(), message);
} else {
Toast.makeText(mContext, "All SMS have been sent",
Toast.LENGTH_SHORT).show();
}
}
private boolean thereAreSmsToSend() {
return mMessageSentCount < array.length;
}
private void sendSMS(final String phoneNumber, String message) {
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(mContext, 0, new Intent(
SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 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() {
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
mContext.getApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
mMessageSentParts++;
if (mMessageSentParts == mMessageSentTotalParts) {
mMessageSentCount++;
sendNextMessage();
}
Toast.makeText(mContext, "SMS sent", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(mContext, "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(mContext, "No service", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(mContext, "Null PDU", Toast.LENGTH_SHORT)
.show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(mContext, "Radio off", Toast.LENGTH_SHORT)
.show();
break;
}
}
}, new IntentFilter(SENT));
mContext.getApplicationContext().registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(mContext, "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(mContext, "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
}
}
Its working fine in OS 4.2.2 but not working with OS 4.4.2 Can you please help me. Thank you guys.
回答1:
By default SMS messages can contain data of 140 bytes. SMS data is sent as a bitstream. This means if you are sending 7 bit ASCII, you can send 160 characters. That's why no longer messages are sent in one. Try to convert your code to send more than one message if the text is longer than 160 characters.
回答2:
package com.example.haider.mysms;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
EditText etmessage, etNumber;
Button btn;
Button btnCall;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etmessage = (EditText) findViewById(R.id.etData);
etNumber = (EditText) findViewById(R.id.reciver);
btn = (Button) findViewById(R.id.send);
btnCall = (Button) findViewById(R.id.call);
btn.setOnClickListener(this);
btnCall.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.send) {
SmsManager smsManager = SmsManager.getDefault();
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);
smsManager.sendTextMessage(etNumber.getText().toString(), null, etmessage.getText().toString(), null, null);
}//end of sms
if (v.getId() == R.id.call) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+etNumber.getText().toString().trim()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}else
this.startActivity(intent);
}//end of sms
}
}
来源:https://stackoverflow.com/questions/30273702/send-sms-in-android-programmatically