2011-10-04 69 views

回答

1

首先讓接收這樣的消息的broadcastreceiver類。 並在收到短信時觸發你班級的意圖。

class SMSReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    Bundle bundle = intent.getExtras(); 
    SmsMessage[] msgs = null; 
    String str = ""; 
    if (bundle != null) { 
     Object[] pdus = (Object[]) bundle.get("pdus"); 
     msgs = new SmsMessage[pdus.length]; 
     for (int i = 0; i < msgs.length; i++) { 
     msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
     str += "SMS from " + msgs[i].getOriginatingAddress(); 
     str += " :"; 
     str += msgs[i].getMessageBody().toString(); 
     str += "\n"; 
     } 
     Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
     Intent mainActivityIntent = new Intent(context, SMS.class); 
     mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     context.startActivity(mainActivityIntent); 
     Intent broadcastIntent = new Intent(); 
     broadcastIntent.setAction("SMS_RECEIVED_ACTION"); 
     broadcastIntent.putExtra("sms", str); 
     context.sendBroadcast(broadcastIntent); 
    } 
    } 
} 

存儲在字符串中作爲sms_sender

String msg_sender=msg[0].getOriginatingAddress(); 

和接收到消息後,得到的條件是

if(msg_sender=(String)"your_unique_number") 
    and fire the intent of your class if this condition is true. 
相關問題