2012. 8. 11. 10:50
안드로이드 - SMS와 문자열 비교 안드로이드 이야기2012. 8. 11. 10:50
728x90
반응형
안드로이드 - sms와 지정한 문자열 비교
1.
public
class
SMSReceiver
extends
BroadcastReceiver {
2.
/** TAG used for Debug-Logging */
3.
protected
static
final
String LOG_TAG =
"SMSReceiver"
;
01.
/** The Action fired by the Android-System when a SMS was received.
02.
* We are using the Default Package-Visibility */
03.
private
static
final
String ACTION =
"android.provider.Telephony.SMS_RECEIVED"
;
04.
05.
// @Override
06.
public
void
onReceive(Context context, Intent intent) {
07.
08.
Log.i(LOG_TAG,
"[inside onReceive] "
);
09.
10.
if
(intent.getAction().equals(ACTION)) {
11.
12.
StringBuilder sb =
new
StringBuilder();
13.
Bundle bundle = intent.getExtras();
14.
15.
if
(bundle !=
null
) {
16.
Object[] pdusObj = (Object[]) bundle.get(
"pdus"
);
17.
SmsMessage[] messages =
new
SmsMessage[pdusObj.length];
18.
for
(
int
i =
0
; i<pdusObj.length; i++) {
19.
messages[i] = SmsMessage.createFromPdu ((
byte
[]) pdusObj[i]);
20.
21.
Log.i(
"aaaaaaa"
, messages[i].getOriginatingAddress()
22.
+
"::"
+ messages[i].getMessageBody());
23.
}
24.
25.
Log.i(LOG_TAG,
"[SMSApp Bundle] "
+ bundle.toString());
26.
27.
28.
String m = messages.toString();
29.
30.
if
(m.equals(
"Where are you?"
))
31.
{
32.
Toast.makeText(context,
"맞았음+_+"
, Toast.LENGTH_SHORT).show();
33.
}
34.
else
35.
{
36.
Toast.makeText(context,
"틀렸음ㅠ_ㅠ"
, Toast.LENGTH_SHORT).show();
37.
}
38.
39.
40.
// Consume this intent, that no other application will notice it.
41.
this
.abortBroadcast();
42.
43.
// Start the Main-Activity
44.
Intent i =
new
Intent(context, SMSActivity.
class
);
45.
// i.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
46.
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
47.
context.startActivity(i);
48.
49.
}
50.
}
51.
}
52.
}
어떤분 블로그에서 수신된 문자를 가져와서 토스트로 보여주는 소스를 발견했는데요
거기다가 제가 그 받은 문자랑 제가 지정해놓은 문자랑 비교해서
맞으면 맞다고 토스트를 띄워주려고 하는데
자꾸 "틀렸음ㅠ_ㅠ" 이렇게만 나옵니다....
아무래도 String m 에 메세지가 들어가지 않는거 같은데
자바 초보라서 어떻게 값을 받아와야 할지 잘 모르겠어요.. 도와주세요ㅠ.ㅠ
2010.10.12 15:45:25
해결했습니다ㅠ.ㅠ!!
단순히 messages.toString();로 하는거 말고
for문을 돌려서 messages[index].getMessageBody();로 해주니까
m에 값이 들어가더군요..
그리고 equals 대신 compareTo로 비교해줘야 띄어쓰기까지 완벽하게 비교가 되더라구요 ㅎㅎ
for(int index=0;index<pdusObj.length;index++)
{
String m = messages[index].getMessageBody();
Log.i("m -> ",m.toString());
if(m.compareTo("Where are you?") == 0)
{
Toast.makeText(context, "맞았음+_+", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "틀렸음ㅠ_ㅠ", Toast.LENGTH_SHORT).show();
}
}
728x90
반응형
'안드로이드 이야기' 카테고리의 다른 글
안드로이드 - 슬라이딩 퍼즐 소스 (0) | 2012.08.11 |
---|---|
안드로이드 - 챠트 라이브러리 (0) | 2012.08.11 |
안드로이드 - 전화 수신 거부 (1) | 2012.08.11 |
안드로이드 - 다른 앱에서 어플 실행 (0) | 2012.08.11 |
안드로이드 - Camera 찍고 -> 현재 날짜.jpg 저장하고 -> 읽어서 보여주기 (0) | 2012.08.10 |
그렇담 m값을 비교하지 말고, 로그로 찍어보던가.. Toast로 찍어보던가 하시면.. 될것 같습니다
String m = messages.toString();
Toast.makeText(context, m, Toast.LENGTH_SHORT).show();