5

I need to measure the send speed of SMS in an Android device. I'm thinking on the below approach. Please let me know if it is correct, or if there is a better solution.

Calculate the time spent sending 10 messages, and calculate the average time spent for sending each. But where is the speed amount here? Does the number of characters increase the time spent?

Here is the code I have written.

    SmsManager smsManager = SmsManager.getDefault();
    long startTime = System.currentTimeMillis();

    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);
    smsManager.sendTextMessage("1234567890",null,text,null,null);

    long endTime = System.currentTimeMillis();

    long result = endTime - startTime;

The result is 742 milliseconds, but I doubt that is correct. It means that each SMS only takes 74 milliseconds.

3
  • just for curiosity, what's wrong with 74ms? is it too long?
    – lelloman
    Commented Aug 4, 2016 at 9:53
  • When I try sending SMS using device, it takes about 1.5 second to send. Commented Aug 4, 2016 at 9:56
  • You are assuming that the call is synchronous which I highly doubt. Most likley the 74ms are just for queuing the SMS in a outgoing queue, not actually establish a radio link with nearest base station and sending the message.
    – mach
    Commented Aug 8, 2016 at 8:14

2 Answers 2

3

The sendTextMessage() method takes for its fourth parameter a PendingIntent that will fire when the message is sent. We can attach the current system time as an extra to the Intent for that PendingIntent, and when it fires, retrieve that extra and compare it to the current system time then. This will give us a decent measure of how long it takes to actually send the message.

We'll use a BroadcastReceiver to handle the PendingIntent, simply retrieving the sent time extra in its onReceive(), and figuring the elapsed time from the current time there.

BroadcastReceiver smsSendReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        long sentTime = intent.getLongExtra("sent_time", 0);
        long elapsedTime = System.currentTimeMillis() - sentTime;
        Toast.makeText(context, elapsedTime + "ms", Toast.LENGTH_SHORT).show();
    }
};

We need to register this Receiver before we send the message. In an Activity or Service:

registerReceiver(smsSendReceiver, new IntentFilter("SMS_SENT"));

In sending our message, we'll kinda "cram" the PendingIntent construction right into the sendTextMessage() call, so we don't inadvertently introduce any unnecessary delay between grabbing the current time, and actually making the call.

SmsManager sm = SmsManager.getDefault();
sm.sendTextMessage("1234567890",
                   null,
                   "Testing...",
                   PendingIntent.getBroadcast(this,
                                              0,
                                              new Intent("SMS_SENT")
                                                  .putExtra("sent_time",
                                                            System.currentTimeMillis()),
                                              0),
                   null);

If desired, this can be run multiple times, and an average figured from the results. Be sure to handle the PendingIntents appropriately if doing so, to ensure that the correct send time is delivered for each broadcast. In the given example, an easy way to do this is to use a different request code - the second argument in getBroadcast() - for each call.

It is advisable to unregister the Receiver when you're done with it, lest you get a nasty message in your logs about a leaked Receiver.

unregisterReceiver(smsSendReceiver);
0
1

Set Broadcast Receiver and update time once you got SMS SENT msg . then calculate each sms sent time . link may help Android SMS Delivery Intent always return -1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.