测量SMS发送速度

时间:2016-08-04 07:36:48

标签: android

我需要测量Android设备中短信的发送速度。我正在考虑以下方法。如果它是正确的,或者是否有更好的解决方案,请告诉我。

计算发送10条消息所花费的时间,并计算发送每条消息所花费的平均时间。但这里的速度在哪里?字符数会增加花费的时间吗?

这是我写的代码。

    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;

result是742毫秒,但我怀疑这是正确的。这意味着每条SMS只需74毫秒。

2 个答案:

答案 0 :(得分:2)

sendTextMessage()方法将第四个参数a PendingIntent用于在发送消息时触发。我们可以将当前系统时间作为额外添加到Intent的{​​{1}},当它触发时,检索该额外内容并将其与当前系统时间进行比较。这将为我们提供一个衡量实际发送消息所需时间的方法。

我们将使用PendingIntent来处理BroadcastReceiver,只需在其PendingIntent中检索额外发送时间,并计算从当前时间开始的经过时间。

onReceive()

我们需要在发送消息之前注册此Receiver。在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(); } }; Activity

Service

在发送我们的消息时,我们会将registerReceiver(smsSendReceiver, new IntentFilter("SMS_SENT")); 构造权限“填入”PendingIntent调用,因此我们不会无意中在抓取当前时间之间引入任何不必要的延迟,实际上打电话。

sendTextMessage()

如果需要,可以多次运行,平均值来自结果。如果这样做,请务必妥善处理SmsManager sm = SmsManager.getDefault(); sm.sendTextMessage("1234567890", null, "Testing...", PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT") .putExtra("sent_time", System.currentTimeMillis()), 0), null); ,以确保为每次广播提供正确的发送时间。在给定的示例中,一种简单的方法是使用不同的请求代码 - PendingIntent中的第二个参数 - 用于每个调用。

建议您在完成后取消注册Receiver,以免在日志中收到有关泄漏的Receiver的恶意消息。

getBroadcast()

答案 1 :(得分:1)

设置广播接收器并在获得SMS SENT消息后更新时间。然后计算每个短信发送时间。链接可以帮助Android SMS Delivery Intent always return -1

相关问题