// 폰번호를 인식하여 문자를 보내달라고 요청
runOnUiThread(new Runnable() {
@Override
public void run() {
final TelephonyManager tm = (TelephonyManager) getActivity()
.getSystemService(Context.TELEPHONY_SERVICE);
if(tm == null)
return;
// 내 폰번호를 받음
String myNumber = tm.getLine1Number();
if(StringUtils.isEmpty(StringUtils.trim(myNumber)))
return;
// KT일경우 폰번호 변환
if(myNumber.startsWith("+82")) {
myNumber = myNumber.replace("+82", "0");
}
_phone.setText(myNumber);
_useSmsReceiver = true;
sendVerification();
}
});
private void sendVerification() {
// 인증번호란에 바로 입력할수 있도록 포커스를 줌
_code.requestFocus();
// SMS 인증문자를 바로 수신해서 결과를 입력해줌
if(_useSmsReceiver)
registerReceiver();
}
private void registerReceiver() {
unregisterReceiver();
final IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(SMSReceiver.ACTION);
_receiver = new SMSReceiver();
registerReceiver(_receiver, intentFilter);
}
private void unregisterReceiver() {
if(_receiver != null) {
unregisterReceiver(_receiver);
_receiver = null;
}
}
private class SMSReceiver extends BroadcastReceiver {
private static final String TAG="SMSReceiver";
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
// 문자만 수신받음
if(intent.getAction().equals(ACTION)) {
final Bundle bundle = intent.getExtras();
if(bundle == null)
return;
final Object[] pdusObj = (Object[])bundle.get("pdus");
if(pdusObj == null)
return;
final SmsMessage[] smsMessages = new SmsMessage[pdusObj.length];
for(int i=0; i<smsMessages.length; i++) {
smsMessages[i] = SmsMessage.createFromPdu((byte[])pdusObj[i]);
final String address = smsMessages[i].getOriginatingAddress();
final String message = smsMessages[i].getDisplayMessageBody();
if(address.equals(CS_NUMBER)) {
String adjustMessage = message.replace("[Web발신]", "");
int begin = adjustMessage.indexOf('[');
int end = adjustMessage.indexOf(']');
// 인증번호를 찾았다
final String codeMessage = adjustMessage.substring(begin+1, end);
runOnUiThread(new Runnable() {
@Override
public void run() {
_code.setText(codeMessage);
confirmVerification();
}
});
unregisterReceiver();
}
}
}
}
}
No comments:
Post a Comment