以编程方式在android中发送短信

时间:2015-05-16 08:54:33

标签: android android-intent smsmanager

我使用下面的代码一次在多个人(联系人)的后台发送短信。 它适用于较小的文本,但它不适用于超过160个字符的文本。我正在接受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));

    }
}

它在OS 4.2.2中运行良好但不适用于OS 4.4.2 你能帮我么。谢谢你们。

2 个答案:

答案 0 :(得分:0)

默认情况下,SMS消息可以包含140个字节的数据。 SMS数据作为比特流发送。这意味着如果您要发送7位ASCII,则可以发送160个字符。这就是为什么不再发送消息的原因。如果文本超过160个字符,请尝试将代码转换为发送多条消息。

答案 1 :(得分:0)

    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

    }
}