尝试一次只发送一条短信

时间:2011-01-23 01:13:30

标签: java android sms

我正在尝试一次发送一条短信。我想发送一个并在发送下一个之前等待它发送。有人可以帮帮我吗

 package com.smith.johnathan.jssms;

//import android.telephony.SmsManager;
import android.telephony.gsm.SmsManager;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import android.util.Log;
import android.widget.Button;

import java.io.*;
import android.util.LogPrinter;

import java.io.*;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.TextView;
import android.os.*;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;
import java.io.*;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class JSSMS extends Activity {

    boolean sendingSMS = false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.Button01);
        button.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(JSSMS.this, "Starting SMS", Toast.LENGTH_LONG)
                        .show();

                String message = "This is Johnathan and this is my new number";
                String number;
                try {

                    BufferedReader buffreader = new BufferedReader(
                            new FileReader(Environment
                                    .getExternalStorageDirectory().toString()
                                    + "/numbers.txt"));

                    int i = 0;
                    while ((number = buffreader.readLine()) != null) {

                        Toast.makeText(JSSMS.this, "Sending text to:" + number,
                                Toast.LENGTH_SHORT).show();
                        if(sendingSMS)
                        {
                            wait(100);
                        }
                        sendingSMS = true;
                        sendSMS(number, message);

                    }

                    buffreader.close();
                } catch (java.io.FileNotFoundException e) {
                    Toast.makeText(JSSMS.this, e.toString(), Toast.LENGTH_SHORT)
                            .show();

                } catch (Exception e) {
                    Toast.makeText(JSSMS.this, e.toString(), Toast.LENGTH_SHORT)
                            .show();
                }

                Toast.makeText(JSSMS.this, "DONE!!", Toast.LENGTH_LONG).show();

            }

        });

    }

    // ---sends an SMS message to another device---
    private void sendSMS(String phoneNumber, String message) {
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
                SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

        // ---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                sendingSMS = false;
                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 sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }

}

1 个答案:

答案 0 :(得分:0)

可能涉及一种技术 a)在外发消息上请求送达回执    (不清楚如何在android API上完成) b)注册接收方以收听收据 c)在继续发送消息之前使用线程等待收据 http://developer.android.com/resources/articles/painless-threading.html