[英]Intercept Incoming SMS Message and Modify it
Is there a way to intercept an incoming SMS message, and then modify it before presenting it to the user? 有没有办法拦截传入的SMS消息,然后在将其呈现给用户之前进行修改?
If yes to any of the above, could you please provide some pointers to it? 如果对上述任何一项是肯定的,你能否提供一些指示?
My preferred-solution priority-order is as follows: 我的首选解决方案优先顺序如下:
Thank you all in advance!! 谢谢大家!!
EDIT: 编辑:
For people wondering what is the purpose of this, basically I would like to put a word as a "label" in the sms depending on the content, so when I view the sms, I can see something like "IMPORTANT: blah blah blah", instead of just "blah blah blah". 对于想知道这个目的是什么的人来说,基本上我想根据内容在短信中加上一个单词作为“标签”,所以当我查看短信时,我会看到类似“重要:等等等等”的内容而不仅仅是“等等等等”。
try out this- //register this class as receiver in manifest file for SMS_RECEIVED intent 尝试这个 - //将此类注册为清单文件中的接收者,用于SMS_RECEIVED意图
public class SmsReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
abortBroadcast();**this is prevent message to deliver to user**
Bundle bundle = intent.getExtras();
if (bundle != null) {
// get sms objects
Object[] pdus = (Object[]) bundle.get("pdus");
if (pdus.length == 0) {
return;
}
// large message might be broken into many
SmsMessage[] messages = new SmsMessage[pdus.length];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pdus.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append(messages[i].getMessageBody());
}
String sender = messages[0].getOriginatingAddress();
String message = sb.toString();
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);//phone number will be your number.
}
}
}
}
Sure! 当然! The EASIEST way on iOS is just to create a trigger on the SMS database - /var/mobile/Library/SMS/sms.db
iOS上的EASIEST方法只是在SMS数据库上创建一个触发器 - /var/mobile/Library/SMS/sms.db
CREATE TRIGGER AFTER INSERT ON message
then update the record! 然后更新记录!
The more advanced way of doing it, is hooking private methods, but I won't go that deep right now, you just need to explore the methods :) 更高级的方法是挂钩私有方法,但我现在不会那么深入,你只需要探索方法:)
BTW, you in any way you NEED a jailbroken device 顺便说一句,你无论如何都需要一个越狱设备
Yes there is a way, but unfortunately since the rollout of KitKat it isn't that easy any more. 是的,有一种方法,但不幸的是,自推出KitKat以来,它已经不那么容易了。 To work on versions > Jelly Bean you must have your application run as the default SMS application, that is to modify and abortBroadcast().
要处理版本> Jelly Bean,您必须将您的应用程序作为默认SMS应用程序运行,即修改和abortBroadcast()。 For 4.3 and below, create a broadcast receiver and do something like the following:
对于4.3及更低版本,创建广播接收器并执行以下操作:
public void onReceive( Context context, Intent intent ) {
// Get the SMS map from Intent
Bundle extras = intent.getExtras();
String messages = "";
if ( extras != null ) {
// Get received SMS array
Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );
// Get ContentResolver object for pushing encrypted SMS to the incoming folder
ContentResolver contentResolver = context.getContentResolver();
for ( int i = 0; i < smsExtra.length; ++i ) {
SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
String body = sms.getMessageBody().toString();
String address = sms.getOriginatingAddress();
// Here is where you modify the body of the message!
messages += "SMS from " + address + " :\n";
messages += body + "\n";
putSmsToDatabase( contentResolver, sms );
}
}
}
private void putSmsToDatabase( ContentResolver contentResolver, SmsMessage sms ) {
// Create SMS row
ContentValues values = new ContentValues();
values.put( ADDRESS, sms.getOriginatingAddress() );
values.put( DATE, sms.getTimestampMillis() );
values.put( READ, MESSAGE_IS_NOT_READ );
values.put( STATUS, sms.getStatus() );
values.put( TYPE, MESSAGE_TYPE_INBOX );
values.put( SEEN, MESSAGE_IS_NOT_SEEN );
try {
values.put( BODY, sms.getMessageBody() ); // May need sms.getMessageBody.toString()
}
catch ( Exception e ) {
e.printStackTrace();
}
// Push row into the SMS table
contentResolver.insert( Uri.parse( SMS_URI ), values );
}
This info was obtained from here 此信息来自此处
Almost forgot...constants.. 差点忘了......常数......
public static final String SMS_EXTRA_NAME = "pdus";
public static final String SMS_URI = "content://sms";
public static final String ADDRESS = "address";
public static final String PERSON = "person";
public static final String DATE = "date";
public static final String READ = "read";
public static final String STATUS = "status";
public static final String TYPE = "type";
public static final String BODY = "body";
public static final String SEEN = "seen";
public static final int MESSAGE_TYPE_INBOX = 1;
public static final int MESSAGE_TYPE_SENT = 2;
public static final int MESSAGE_IS_NOT_READ = 0;
public static final int MESSAGE_IS_READ = 1;
public static final int MESSAGE_IS_NOT_SEEN = 0;
public static final int MESSAGE_IS_SEEN = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.