public class SMSReceiver extends BroadcastReceiver {
/** TAG used for Debug-Logging */
protected static final String LOG_TAG = "SMSReceiver";
/** The Action fired by the Android-System when a SMS was received.
* We are using the Default Package-Visibility */
private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
// @Override
public void onReceive(Context context, Intent intent) {
Log.i(LOG_TAG, "[inside onReceive] ");
if (intent.getAction().equals(ACTION)) {
StringBuilder sb = new StringBuilder();
Bundle bundle = intent.getExtras();
if (bundle != null) {
Object[] pdusObj = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdusObj.length];
for (int i = 0; i<pdusObj.length; i++) {
messages[i] = SmsMessage.createFromPdu ((byte[]) pdusObj[i]);
Log.i("aaaaaaa", messages[i].getOriginatingAddress()
+ "::" + messages[i].getMessageBody());
}
Log.i(LOG_TAG, "[SMSApp Bundle] " + bundle.toString());
String m = messages.toString();
if(m.equals("Where are you?"))
{
Toast.makeText(context, "맞았음+_+", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(context, "틀렸음ㅠ_ㅠ", Toast.LENGTH_SHORT).show();
}
// Consume this intent, that no other application will notice it.
this.abortBroadcast();
// Start the Main-Activity
Intent i = new Intent(context, SMSActivity.class);
// i.setLaunchFlags(Intent.NEW_TASK_LAUNCH);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
}
어떤분 블로그에서 수신된 문자를 가져와서 토스트로 보여주는 소스를 발견했는데요
거기다가 제가 그 받은 문자랑 제가 지정해놓은 문자랑 비교해서
맞으면 맞다고 토스트를 띄워주려고 하는데
자꾸 "틀렸음ㅠ_ㅠ" 이렇게만 나옵니다....
아무래도 String m 에 메세지가 들어가지 않는거 같은데
자바 초보라서 어떻게 값을 받아와야 할지 잘 모르겠어요.. 도와주세요ㅠ.ㅠ
해결했습니다ㅠ.ㅠ!!
단순히 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();
}
}




그렇담 m값을 비교하지 말고, 로그로 찍어보던가.. Toast로 찍어보던가 하시면.. 될것 같습니다
String m = messages.toString();
Toast.makeText(context, m, Toast.LENGTH_SHORT).show();