package com.thirtysparks.apple.bot;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.support.v4.content.LocalBroadcastManager;
import android.telephony.SmsMessage;
import android.util.Log;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Kelly Lau on 30/10/2014.
 */
public class ReceiveSmsBroadcastReceiver extends BroadcastReceiver {
    private static final String TAG = "ReceiveSmsBroadcastReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {

        if (null != intent) {
            Bundle bundle = intent.getExtras();
            Log.d(TAG, "Received SMS intent");

            if (null != bundle) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                SmsMessage[] smsMessage = new SmsMessage[pdus.length];
                String [] allMessageContent = new String[pdus.length];

                for (int i = 0; i < pdus.length; i++) {
                    smsMessage[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    allMessageContent[i] = smsMessage[i].getMessageBody();
                    for(String message:allMessageContent){
                        if(message != null) {
                            String smsPattern = "你的註冊代碼為 (Your registration code is) ";
                            int idx = message.indexOf(smsPattern);
                            if(idx > -1){
                                String smsCode = message.substring(idx + smsPattern.length());
                                Log.d(TAG, "Matched SMS code: " + smsCode);
                            }
                        }
                    }
                }
            }
        }
    }

    private void broadcastMessageToActivity(Context context, String msg) {
        Intent in = new Intent(MainActivity.BROADCAST_RECEIVE_SMS);
        in.putExtra(MainActivity.KEY_RECEIVE_SMS_RESULT, msg);
        LocalBroadcastManager.getInstance(context).sendBroadcast(in);
    }
}

@@ -1388,16 +1388,18 @@
 %E4%BD%A0%E7%9A%84%E8%A8%BB%E5%86%8A%E4%BB%A3%E7%A2%BC%E7%82%BA 
+%5C%5C
 (Your re
@@ -1416,18 +1416,34 @@
  code is
+%5C%5C) (%5Ba-zA-Z0-9%5D+
 )
- 
 %22;%0A     
@@ -1469,33 +1469,41 @@
     
-int idx = message.indexOf
+Pattern pattern = Pattern.compile
 (sms
@@ -1544,20 +1544,100 @@
     
-if(idx %3E -1)
+Matcher matcher = pattern.matcher(message);%0A                            if (matcher.find()) 
 %7B%0A  
@@ -1680,16 +1680,23 @@
  sms
+Respond
 Code = m
 essa
@@ -1695,50 +1695,22 @@
  = m
-essage.substring(idx + smsPattern.length()
+atcher.group(1
 );%0A 
@@ -1777,16 +1777,109 @@
  %22 + sms
+RespondCode);%0A%0A                                broadcastMessageToActivity(context, smsRespond
 Code);%0A 

Extract the sms code by regex