2016-08-26 8 views
0

着信SMSからのワンタイムパスワードを自動的に読み取るアプリを開発しようとしています。私はSMSというクラスを持っています。ここには現在読んでいるメッセージと、可能なOTP送信者に関する情報を含むSMSRuleというクラスがあります。MatcherはAndroidのパターンと一致しません

SMSRuleには、実際にOTPの正しい送信者であるかどうかを確認するために、メッセージに一致させる必要のある変数が含まれています(Pattern)。

メッセージからのOTP抽出を除いてすべてがスムーズに進んでいます。 SMSにメッセージが含まれていますが、Patternとメッセージを照合しようとすると、match.find()はfalseを返します。続き

が私のファイルです:

OTPAutoRead.java

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.content.pm.PackageManager; 
import android.os.Bundle; 
import android.support.annotation.NonNull; 
import android.telephony.SmsMessage; 
import android.util.Log; 
import android.widget.Toast; 

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

import auro.widget.OTP.SMS.SMS; 
import auro.widget.OTP.SMS.SMSRule; 

/** 
* Created on 26/8/16. 
* @author Auro 
* @since 1.0 
*/ 

public class OTPAutoRead extends BroadcastReceiver { 

    private static final String LOG_TAG = OTPAutoRead.class.getCanonicalName(); 

    private static final String mReadPermission = "android.permission.READ_SMS"; 
    private static final String mReceivePermission = "android.permission.RECEIVE_SMS"; 

    private List<SMSRule> smsRules; 
    private Context context; 
    private String OTP; 

    public OTPAutoRead() {throw new InstantiationError("Empty Constructor");} 

    public OTPAutoRead(@NonNull Context context, final List<SMSRule> smsRules) { 

     if (smsRules == null || smsRules.size() == 0) { 

      throw new AuroMissingRulesException("No SMS Rules Found"); 
     } 

     this.smsRules = smsRules; 
     this.context = context; 
     CheckSMSReadPermission(); 

     IntentFilter itf = new IntentFilter(); 
     itf.addAction("android.provider.Telephony.SMS_RECEIVED"); 
     itf.setPriority(999); 
     context.registerReceiver(this,itf); 

    } 


    private void CheckSMSReadPermission() { 

     PackageManager packageManager = context.getPackageManager(); 
     String packageName = context.getPackageName(); 

     int readPermission = packageManager.checkPermission(mReadPermission,packageName); 
     int receivePermission = packageManager.checkPermission(mReceivePermission, packageName); 

     boolean canRead = (readPermission == PackageManager.PERMISSION_GRANTED); 

     if (!canRead) 
      Toast.makeText(context,"Please enable SMS read permission for auto OTP read", Toast.LENGTH_SHORT).show(); 
    } 


    @Override 
    public void onReceive(Context context, Intent intent) { 

     try { 
      if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) 
       tryReceiveMessage(intent); 
     } catch (Exception e) { 
      Log.i(LOG_TAG, "Failed to read SMS", e); 
     } 
    } 


    private void tryReceiveMessage(Intent intent) { 

     Bundle bundle = intent.getExtras(); 

     SmsMessage[] messages = null; 

     if (bundle != null) { 

      Object[] pdus = (Object[]) bundle.get("pdus"); 

      if (pdus != null) { 
       messages = new SmsMessage[pdus.length]; 

       for (int i = 0; i < messages.length; i++) { 

        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
        String messageFrom = messages[i].getOriginatingAddress().toUpperCase(); 
        String messageBody = messages[i].getMessageBody().toUpperCase(); 

        Log.d(LOG_TAG, "Message is from: " + messageFrom + " and its content is: " + messageBody); 

        SMS sms = new SMS(); 
        sms.setMessage(messageBody).setAddress(messageFrom); 

        processOTP(sms); 
       } 
      } 
     } 
    } 


    private void processOTP(SMS sms) { 

     int i; 

     for (i = 0; i < smsRules.size(); i ++) { 

      if (sms.getAddress().toUpperCase().contains(smsRules.get(i).getSender().toUpperCase())) { 

       Pattern pattern = smsRules.get(i).getOTPPattern(); 

       System.out.println(pattern.pattern()); 
       System.out.println(sms.getMessage()); 

       Matcher matcher = pattern.matcher(sms.getMessage().toUpperCase()); 

       if (matcher.find()) { 

        OTP = matcher.group(1); 
        Log.i(LOG_TAG,"Extracted OTP is: " + OTP); 
        Toast.makeText(context,"OTP RECEIVED IS: " + OTP,Toast.LENGTH_SHORT).show(); 
        return; 
       } else 
        Log.d(LOG_TAG,"Failed to extract OTP"); 
      } 
     } 
    } 

    public String getOTP() { 
     return OTP; 
    } 

} 

SMS.java

import android.support.annotation.NonNull; 

/** 
* Created on 26/8/16. 
* @author Auro 
* @since 1.0 
*/ 
public class SMS { 

    private String mID; 
    private String mAddress; 
    private String mMessage; 
    private boolean isRead = false; 
    private String mTime; 

    public String getID() { 

     return mID; 
    } 

    public SMS setID(@NonNull final String ID) { 

     mID = ID; 
     return this; 
    } 

    public String getAddress() { 

     return mAddress; 
    } 

    public SMS setAddress(@NonNull final String Address) { 

     mAddress = Address; 
     return this; 
    } 

    public String getMessage() { 
     return mMessage; 
    } 

    public SMS setMessage(@NonNull final String Message) { 

     mMessage = Message; 
     return this; 
    } 

    public boolean isRead() { 

     return isRead; 
    } 

    public SMS setReadState(boolean ReadState) { 

     isRead = ReadState; 
     return this; 
    } 

    public String getTime() { 

     return mTime; 
    } 

    public SMS setTime(@NonNull String Time) { 

     mTime = Time; 
     return this; 
    } 
} 

SMSRule.java

import java.util.regex.Pattern; 

/** 
* Created on 26/8/16. 
* @author Auro 
* @since 1.0 
*/ 
public class SMSRule { 

    private String mSender; 
    private String mGroupID; 
    private Pattern mOTPPattern; 


    private SMS sms; 

    public String getSender() { 

     return mSender; 
    } 

    public SMSRule setSender(final String sender) { 

     mSender = sender; 
     return this; 
    } 

    public String getGroupID() { 

     return mGroupID; 
    } 

    public SMSRule setGroupID(final String groupID) { 

     mGroupID = groupID; 
     return this; 

    } 


    public Pattern getOTPPattern() { 

     return mOTPPattern; 
    } 

    public SMSRule setOTPPattern(final Pattern otpPattern) { 

     mOTPPattern = otpPattern; 
     return this; 
    } 

} 

MainActivity.java

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.widget.EditText; 

import java.util.ArrayList; 
import java.util.List; 
import java.util.regex.Pattern; 

import auro.widget.OTP.OTPAutoRead; 
import auro.juspay.widget.OTP.SMS.SMSRule; 


public class MainActivity extends AppCompatActivity { 


    private EditText editText; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     editText = (EditText) findViewById(R.id.editText); 

     SMSRule rule = new SMSRule(); 
     Pattern pattern = Pattern.compile("One Time Password is (\\d{6})".toUpperCase()); 

     rule.setSender("BANK-XYZ"); 
     rule.setOTPPattern(pattern); 

     List<SMSRule> smsRules = new ArrayList<>(); 
     smsRules.add(rule); 

     OTPAutoRead otpAutoRead = new OTPAutoRead(this,smsRules); 

     String OTP = otpAutoRead.getOTP(); 

     if (OTP != null) 
     { 
      editText.setText(OTP); 
     } 
    } 

} 

問題は、私はDebugモードでこれを実行すると、私はmatchFound = falseを取得し、です。あなたが大文字にパターン文字列を変換する場合

enter image description here

+1

記録のために:あなたは非常に多くのコードを投稿しています。ヘルプセンターで**ミニマム**の例について説明したアイデアについて聞いたことがありますか?投稿するだけでは十分ではないでしょうか?A)予想される入力B)あなたのパターンC)何が起こるのですか?あなたはスクリーンショットを投稿しません。 – GhostCat

+0

リテラルであるパターンの部分に対してtoUpperCaseを使用します。 '\ d {6}'や\ r、\ n、\ tのような正規表現の構成部分では使用しないでください。また、Java言語では、文字列を二重にエスケープする必要がありますので、toUpperCase())+ "\\ d {6}" ' – sln

答えて

1

- :私もそれはちょうど後

を動作しないでしょうregexPattern pattern = Pattern.compile("One Time Password is \d{6}".toUpperCase());

を変更するとスクリーンショットです試してみましたPattern.compile("One Time Password is (\\d{6})".toUpperCase())\d(数字と一致)は\D(数字と一致しません)に変わります。代わり\d

使用[0-9]パターンが同じ手段、または単にtoUpperCase()を除去し、正確なパターンを書き込む、またはパターンの大文字と小文字を区別しないフラグを使用するように。

関連する問題