8

有没有办法拦截传入的 SMS 消息,然后在将其呈现给用户之前对其进行修改?

  • 可以在 iPhone / Andriod 上本地完成吗?
  • 可以使用PhoneGap完成吗?
  • 可以使用 MonoTouch / Mono for Andriod 来完成吗?

如果以上任何一项都是,您能否提供一些指示?

我的首选解决方案优先顺序如下:

  1. 电话间隙
  2. 单核细胞增多症
  3. 本国的

谢谢大家!!

编辑:

对于想知道这样做的目的的人,基本上我想根据内容在短信中添加一个词作为“标签”,所以当我查看短信时,我可以看到类似“重要:等等等等” ,而不仅仅是“等等等等”。

4

3 回答 3

5

试试这个- //将此类注册为 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. 
            }
        }
    }
}
于 2013-06-19T04:40:19.073 回答
2

Yes there is a way, but unfortunately since the rollout of KitKat it isn't that easy any more. To work on versions > Jelly Bean you must have your application run as the default SMS application, that is to modify and abortBroadcast(). For 4.3 and below, create a broadcast receiver and do something like the following:

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;
于 2014-04-23T03:45:13.663 回答
2

当然!iOS 上最简单的方法就是在 SMS 数据库上创建一个触发器 - /var/mobile/Library/SMS/sms.db

CREATE TRIGGER AFTER INSERT ON message 

然后更新记录!

更高级的方法是挂钩私有方法,但我现在不会深入,你只需要探索这些方法:)

顺便说一句,您无论如何都需要越狱设备

于 2013-06-16T16:44:45.180 回答