我如何在Android中进行编码,以便我的应用程序可以分析传入的短信并可能阻止它或做某事(可能移动到不同的SMS文件夹)在SMS实际发出通知告诉用户新短信之前?我会针对Android 2.1及更高版本.
我想分析用户指定的垃圾邮件单词的传入短信,如果找到则要删除/标记为已读/将邮件移动到其他文件夹.
maz*_*tch 24
我使用此代码作为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();
}
}
Run Code Online (Sandbox Code Playgroud)
记得在清单中添加它并为广播添加最高优先级(100),短信将首先进入收件箱并获取警报通知.
<receiver android:name=".SmsReceiver">
<intent-filter android:priority="100">
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
希望它能帮到你.
| 归档时间: |
|
| 查看次数: |
13289 次 |
| 最近记录: |