1

我正在开发一个应用程序,该应用程序假设捕获手机中任何 SMS 应用程序发送的 SMS 的发送报告。

我曾在发送和收集交付报告的消息传递应用程序上工作过。这是通过设置待定意图来完成的:

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 以获取信息,但我不确定。我试图在网上搜索,但找不到与我需要相关的东西。

帮帮我谢谢。

4

1 回答 1

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();
}
于 2014-01-10T16:59:26.850 回答