Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试一次只发送一条SMS消息_Java_Android_Sms - Fatal编程技术网

Java 尝试一次只发送一条SMS消息

Java 尝试一次只发送一条SMS消息,java,android,sms,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.Int

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

 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);
    }

}

一种技术可能包括 a) 请求发送邮件的送达回执 (不清楚如何在android API上实现这一点) b) 注册接收者以监听收据 c) 在继续发送消息之前,使用线程等待接收

短信息不是会被触发并遗忘吗?我认为发送手机会得到一个响应,即消息在网络中;但如果网络真的把它交给了接收者,就不会了。这基本上是正确的。但是,这似乎暗示可以在文本消息本身中设置标志,以便在目标手机收到消息时返回回复消息。