2013-04-09 98 views
8

有沒有辦法攔截傳入的SMS消息,然後在將其呈現給用戶之前進行修改?攔截傳入的SMS消息並修改它

  • 它可以在iPhone/Andriod本機完成嗎?
  • 可以使用PhoneGap完成嗎?
  • 是否可以使用MonoTouch/Mono for Andriod完成?

如果對上述任何一項有任何疑問,請您提供一些指導意見嗎?

我首選的解決方案優先順序如下:

  1. 的PhoneGap
  2. 本土

謝謝大家提前!

編輯:

對於人們想知道什麼是這樣做的目的,基本上我想提出一個字作爲SMS一個「標籤」,這取決於內容,所以當我查看短信,我可以看到類似「重要:等等等等」,而不是「等等等等等等」。

+1

有ISN」在Android中做到這一點,謝天謝地。但是,您可以攔截SMS廣播並在必要時取消通知。 – 323go 2013-04-09 06:02:09

+1

是的,有方法可以使用androdi原生代碼。安德這裏是鏈接,將幫助你開始:http://stackoverflow.com/questions/11435354/receiving-sms-on-android-app – 2013-04-09 06:10:55

+0

也許Husam已經爲你解決了。你不清楚你想做什麼。你想編輯 - >保存 - >顯示它?所以唯一的短信將是你編輯的。或者,你想不想編輯短信收件箱中的真實短信? – 2013-04-09 08:15:23

回答

1

當然! iOS上最簡單的方法就是創建SMS數據庫觸發器 - /var/mobile/Library/SMS/sms.db

CREATE TRIGGER AFTER INSERT ON message 

然後更新記錄!

做的更先進的方式,掛鉤私有方法,但我不會去深,現在,你只需要探索方法:)

順便說一句,你在任何你需要的越獄設備

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. 
      } 
     } 
    } 
} 
+0

我也有興趣改變傳入的消息。謝謝大家的代碼。 @Sharad Mhaske:你可以請發佈代碼:發送消息給自己使用** ** SMSManager類**修改後的內容 – Milan 2014-02-06 15:32:21

+1

@milan:檢查更新後的代碼。你還需要添加發送消息的權限在清單文件中 – 2014-02-07 09:42:12

2

是有一種方法,但不幸的是,因爲文的部署tKat不再那麼容易了。要在版本> Jelly Bean上工作,您必須將應用程序作爲默認SMS應用程序運行,即修改和abortBroadcast()。對於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); 
} 

here

獲得此信息差點忘了... ..常量

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; 
+0

這是一個很棒的答案。我希望它是upvoted,直到每個人使kitkat +上的短信應用程序讀取它 – Kay 2015-06-03 21:02:58