如何为Android上任何短信应用发送的短信生成发送报告

时间:2012-04-30 10:48:03

标签: android sms broadcastreceiver messaging

我正在开发一款应用程序,该应用程序可以捕获手机中任何短信应用发送的短信发送报告。

我曾致力于发送并收集投放报告的消息传递应用程序。这是通过设置待定意图来完成的:

PendingIntent deliveredPI = PendingIntent.getBroadcast(this, id, intent_delivery, 0);

然后

registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                Bundle extras = new Bundle();
                extras = arg1.getExtras();
                switch (getResultCode()) {
                case Activity.RESULT_OK:
                    notifyMessage("SMS delivered", getBaseContext(), extras.getInt("id"));

                    break;
                case Activity.RESULT_CANCELED:
                    notifyMessage("SMS not delivered", getBaseContext(), extras.getInt("id"));

                    break;
                default :
                    notifyMessage("Unable to generate delivery Report", getBaseContext(), extras.getInt("id"));
                }
            }
        }, new IntentFilter(DELIVERED));

但现在我想知道如何收集手机上安装的任何短信应用发送的短信。

目前我只是想为此目的设置一个广播列表器,但我不知道如何设置和使用什么样的广播列表器以及检查wht参数。我想我必须检查PDU的信息,但我不确定。我试图搜索网络,但我找不到与我相关的东西。

帮帮我 谢谢。

1 个答案:

答案 0 :(得分:1)

在您的活动类中,声明这些全局变量:

private BroadcastReceiver sendBroadcastReceiver;
private BroadcastReceiver deliveryBroadcastReceiver;
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";

覆盖你的活动的onCreate并注册你的接收者:

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  sendBroadcastReceiver = new BroadcastReceiver()
  {

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

deliveryBroadcastReceiver = new BroadcastReceiver()
{
    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;
        }
    }
};
registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT));
}

接下来,您的发送短信方法只剩下6行代码:

public 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);
   SmsManager sms = SmsManager.getDefault();
   sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}

最后,在停止活动时取消注册您的接收器:

@Override
protected void onStop()
{
  unregisterReceiver(sendBroadcastReceiver);
  unregisterReceiver(deliveryBroadcastReceiver);
  super.onStop();
}