gpt4 book ai didi

android - 如何在 Android 上分析传入的短信?

转载 作者:IT王子 更新时间:2023-10-28 23:30:53 38 4
gpt4 key购买 nike

我如何在 Android 中进行编码,以便我的应用可以分析传入的 SMS,并可能在 SMS 实际发出通知通知用户有新 SMS 之前阻止它或执行某些操作(可能移动到不同的 SMS 文件夹)?我会针对 Android 2.1 及更高版本。

我想分析传入的 SMS 以查找用户指定的垃圾邮件词,如果发现我想删除/标记为已读/将消息移动到不同的文件夹。

最佳答案

我使用这段代码,作为广播接收器:

public void onReceive(Context context, Intent intent) 
{
//this stops notifications to others
this.abortBroadcast();

//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
from = msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
msg = msgs[i].getMessageBody().toString();
str += "\n";
}
if(checksomething){
//make your actions
//and no alert notification and sms not in inbox
}
else{
//continue the normal process of sms and will get alert and reaches inbox
this.clearAbortBroadcast();
}
}

记得将它添加到 list 中并添加广播的最高优先级 (100),否则短信将首先进入收件箱并收到警报通知。

    <receiver android:name=".SmsReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>

希望对你有帮助。

关于android - 如何在 Android 上分析传入的短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4637821/

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