如何分析接收到的SMS在Android? [英] How to analyze incoming SMS on Android?
问题描述
我怎样才能code在Android中,这样我的应用程序可以分析传入的短信,也许阻止它或者做了什么(可能移动到不同的SMS文件夹)前的短信实际上提出了一个通知,告诉新用户短信?我将瞄准了Android 2.1及以上。
How can I code in Android such that my app can analyze an incoming SMS and perhaps block it or do something(maybe move to a different SMS folder) BEFORE the SMS actually raises a notification telling the user of a new SMS? I would target Android 2.1 and above.
我要分析接收到的SMS用户指定的垃圾邮件的话,如果发现将要删除/标记为已读/移动邮件到其他文件夹。
I would want to analyse incoming SMS for user specified spam words, and if found would want to delete/mark as read/move the message to a different folder.
推荐答案
我用这个code,作为一个BroadcastReceiver:
I use this code, as a BroadcastReceiver:
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();
}
}
记得将其添加到清单和广播或短信添加higgest优先级(100)会先去收件箱并获得警报通知。
remember to add it in manifest and add a higgest priority (100) for broadcast or sms will go first to inbox and get the alert notification.
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
希望它可以帮助你。
Hope it helps you.
这篇关于如何分析接收到的SMS在Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!