开发者

Sending out SMS messages from my Android phone in Java, how can I do them one at a time?

开发者 https://www.devze.com 2023-02-06 05:48 出处:网络
Sending out SMS messages from my Android phone in Java, how can I do them one at a time? I made this application to send out a number of SMS messages from my phone, but how do I change it to send one

Sending out SMS messages from my Android phone in Java, how can I do them one at a time?

I made this application to send out a number of SMS messages from my phone, but how do I change it to send one at a time?

I am trying to make this send a SMS message and wait for the reply code to get back before sending the next one:

import android.telephony.gsm.SmsManager;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.开发者_JAVA百科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 {
    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 = "Hello This Is John, Please save 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();
                        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) {
                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);
    }
}


Here's a general hint to set you in the right direction. When you have a queue of items you want to process in some asynchronous manner, one approach is use the callback/event handler/call-it-what-you-will to trigger the processing of the next element in the queue.

  • set up your queue of messages
  • have a method, e.g. 'sendNextMessage' which pops the top message off the queue and sends it
  • now, in your onReceive method, you can call your sendNextMessage to trigger sending the next one in the queue

For this approach to work, you need to ensure you are guaranteed to get some kind callback regardless of success or failure.


Look at the sendSms() method in this class.

When you call sendTextMessage(), you can pass a "sent" and a "delivered" intent. Target these intents back to your own application. You can have a callback so to speak when the SMS is sent or delivered.

If you look at the AndroidManifest.xml file in that same project as the references source file, you can see there are two receivers defined that register for "sms_sent" (and "sms_received") intents. If you look at the method I mentioned, you can see that before calling sendTextMessage() it creates two intents with the action "sms_sent" and "sms_delivered". This is how you get notified when an SMS is done sending.


First thing first there are four to five steps

Step 1 open button click or any trigger u start sms sending

Step 2 create Pending Intent to be broadcasted with the intent object and OFCOURSE WITH MULTIPLE PENDING INTENT... for multiple sms sending capability...

Step 3 the registered broadcast receiver will receive the pending intent

Step 4 based on the result and the received INTENT, I REPEAT THE INTENT object u send the next message ... here is a code for it

i trust it's readable enough... stay awesome

public class SMSDoer extends Activity {
 String SENT = "SMS_SENT";
 String DELIVERED = "SMS_DELIVERED";
 PendingIntent sentPI, deliveredPI;
 BroadcastReceiver smsSentReceiver, smsDeliveredReceiver;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

}

@Override
public void onResume() {
super.onResume();
//---create the BroadcastReceiver when the SMS is sent---
smsSentReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
switch (getResultCode())
{
 case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), “SMS Gone With the wind”,
//db.query update database for recurrisive message calling 
//using intent object for specific message
sendSMS(“db.phoneNumber", "db.msg");

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



//---create the BroadcastReceiver when the SMS is delivered---
smsDeliveredReceiver = 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;
}
}
};
//---register the two BroadcastReceivers---
registerReceiver(smsDeliveredReceiver, new IntentFilter(DELIVERED));
registerReceiver(smsSentReceiver, new IntentFilter(SENT));
}

@Override
public void onPause() {
super.onPause();
//---unregister the two BroadcastReceivers---
unregisterReceiver(smsSentReceiver);
unregisterReceiver(smsDeliveredReceiver);
}
public void SmsMaker(View v) {
//query database
sendSMS(“db.phoneNumber", "db.msg", db.row);
}
//---sends an SMS message to another device---
private void sendSMS(String phoneNumber, String message, int rowId)
{
//create multiple Pending intent
sentPI = PendingIntent.getBroadcast(this,rowId, new Intent(SENT), 0);
deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0);

SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, "+29170000017", message,  sentPI,deliveredPI);
}
}
0

精彩评论

暂无评论...
验证码 换一张
取 消