2011-12-15 1 views
0

私はここで助けが必要ですが、前に研究をしましたが、まだ正確な解決策を得ることはできません。
私はこのコードをここに持っています。
メッセージを受信できますが、特定のコード/テキストを含む特定のSMSメッセージを受信するようにAndroidに設定してください。コード/テキストが検出されている場合、サーブレットを含むjava WebアプリケーションにWebリクエストを送信します。それを変更する方法についての助けがあれば教えてください。
使用方法:Eclipse Indigo 2.7、API 2.3.3
ご協力いただければ幸いです。
ありがとうございます。特定のコードまたはテキストを含むSMSを受信し、Webリクエストを送信します。

@SuppressWarnings("deprecation") 
public class SmsReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     //---get the SMS message passed in--- 
     Bundle bundle = intent.getExtras(); 
     [] msgs = null; 
     String str = ""; 
     if (bundle != null) { 
      //---retrieve the SMS message received--- 
      Object[] pdus = (Object[]) bundle.get("pdus"); 


      msgs = new [pdus.length]; 
      for (int i=0; i < msgs.length; i++) { 
       msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); 
       str += "SMS from " + msgs[i].getOriginatingAddress(); 
       str += " :"; 
       str += msgs[i].getMessageBody().toString(); 
       str += "\n"; 
      } 

      //---display the new SMS message--- 
      Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
     } 
    } 
} 

テキストにあなたがいるパターンのどのような種類=/

public static void main(String[] args) { 

String number = "123345"; 

//Direct use of Pattern: 
Pattern p = Pattern.compile("\\d+"); 
Matcher m = p.matcher(number); 

while (m.find()) { // Find each match in turn; String can't do this. 
    String result = m.group(); // Access a submatch group; String can't do this. 
    System.out.println("Result: "+result); 
    } 
} 

}

答えて

0

私は(下記のように)正規表現の例をしましたが、私は上記のコードにそれを実装について困惑しています探している?特別なテキストの検出にはString.contains()を使用することができます。送信する場合はHttpPostクラスを使用できます。

+0

ありがとうございます! =)例の場合、文字列である "123456"のような特定のパスワードを含むSMSを検出したいと考えています。私はアンドロイドに新しいので、私はどのメソッドを使用するかについて、混乱しています。 – aliciakng

+0

パスワードと電話番号の違いは何ですか?たぶんキーワードや特別な送信者があるはずですか? – Jin35

+0

おそらく彼らは文字列として渡すことができるので、違いはありません。うん、いくつかのキーワードかもしれない。正規表現を使用して一致させることはできますか? – aliciakng

関連する問題