2012-03-05 121 views
-2

我在android應用程序的短信中遇到了一個小問題。我需要您的幫助來接收我向其發送消息的配方地址。短信android接收函數

示例代碼如下。

public void oncall() { 
     // public void onReceive(Context context, Intent intent) { 
     // ---get the SMS message passed in--- 
     Bundle bundle = new Bundle(); // intent.getExtras(); 

     SmsMessage[] msgs; 
     // String str = ""; 
     if (bundle != null) { 
      // ---retrieve the SMS message received--- 
      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]); 
       String Mobno = msgs[i].getOriginatingAddress(); 
       // String message = msgs[i].getMessageBody().toString(); 
       output.setText("" + Mobno); 
      } 
      // ---display the new SMS message--- 
      // Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
     } 
    } 
+0

什麼錯誤u的越來越u的使用字符串Mobno =封郵件[I] .getOriginatingAddress();你將獲得手機號碼 – user1203673 2012-03-05 07:07:39

+1

沒有錯誤的描述... -1。 – JoxTraex 2012-03-05 07:16:49

+0

我在做類似的事情! http://stackoverflow.com/questions/14452808/sending-and-receiving-mms-in-android – toobsco42 2013-01-22 08:13:07

回答

1
 Bundle bundle = intent.getExtras(); 
    if (bundle != null) 
{ 
    Object[] pdus = (Object[]) bundle.get("pdus"); 
    for (Object pdu : pdus) 
{ 
    SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu); 

if(smsMessage.getOriginatingAddress() != null) 
{ 
String num = smsMessage.getOriginatingAddress(); 

} 
} 
} 
+0

我錯了我的問題。我爲此道歉。我懷疑我是在單一活動中創造2個功能。一個函數是send(),另一個函數是receive()消息。我需要獲取我向其發送消息的手機號碼。 – user1239393 2012-03-05 07:21:08

+0

上述代碼用於實現SmsReceiver類的onReceive方法,它擴展了BroadcastReceiver – 2012-03-05 07:30:33