如果短信来自特定号码,我有一个 if 语句应该触发,但它永远不会触发。这是我的 SMSReceiver.java:
public String SMSMessage;
public String incomingNumber;
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(SMS_RECEIVED))
{
abortBroadcast();
Bundle bundle = intent.getExtras();
if (bundle != null)
{
Object[] pdus = (Object[])bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
{
messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
}
SMSMessage = messages[0].getMessageBody();
incomingNumber = messages[0].getOriginatingAddress();
MainActivity.updatePowerUsageTitle(SMSMessage);
MainActivity.updatePowerUsageSummary(incomingNumber);
if (incomingNumber == "XXXXXXXXX" || incomingNumber == "XXXXXXXXXX")
{
Toast.makeText(context, messages[0].getMessageBody(), Toast.LENGTH_SHORT).show();
if (SMSMessage.contains("Turn"))
{
//TODO: Set toast for incoming message
Toast.makeText(context, messages[0].getMessageBody(), Toast.LENGTH_SHORT).show();
}
else if (SMSMessage.contains("ref"))
{
//TODO: Set refresh for switches
}
else if (SMSMessage.contains("power"))
{
//Refresh power stats
int END = SMSMessage.length();
SMSMessage = SMSMessage.substring(6 , END);
MainActivity.updatePowerUsageTitle(SMSMessage);
}
}
}
}
}
这是重要的部分:
incomingNumber = messages[0].getOriginatingAddress();
if (incomingNumber == "XXXXXXXXX" || incomingNumber == "XXXXXXXXXX")
{
Toast.makeText(context, messages[0].getMessageBody(), Toast.LENGTH_SHORT).show();
}
很明显,我没有把电话号码放在这里,但我知道号码是正确的。关于为什么 if 语句没有触发的任何线索?