gpt4 book ai didi

android - 如何为 Android 上的任何 SMS 应用程序发送的 SMS 生成送达报告

转载 作者:行者123 更新时间:2023-11-30 04:13:54 26 4
gpt4 key购买 nike

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

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

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

帮帮我
谢谢你。

最佳答案

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

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

覆盖您的 Activity 的 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);
}

最后,在您的 Activity 停止时取消注册您的接收器:
@Override
protected void onStop()
{
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
super.onStop();
}

关于android - 如何为 Android 上的任何 SMS 应用程序发送的 SMS 生成送达报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10382405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com