2012-04-05 84 views
0

我想從我們的應用程序發送和接收短信,併發送成功,但接收顯示在我的網站上展示的代碼的SmsMessage類中的顯示棄用。所以,我想要一個接收方法來接收短信,這是不被棄用的。請爲我提供正確的解決方案。如何從我們的android應用程序接收短信?

接收代碼:

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.telephony.gsm.SmsMessage; 
import android.widget.Toast; 

public class SmsReceiver extends BroadcastReceiver 
{ 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     //---get the SMS message passed in--- 
     Bundle bundle = intent.getExtras();   
     SmsMessage[] msgs = null; 
     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]);     
       str += "SMS from " + msgs[i].getOriginatingAddress();      
       str += " :"; 
       str += msgs[i].getMessageBody().toString(); 
       str += "\n";   
      } 
      //---display the new SMS message--- 
      Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
     }       
    } 
} 
+0

我做類似的東西在這裏! http://stackoverflow.com/questions/14452808/sending-and-receiving-mms-in-android – toobsco42 2013-01-22 08:28:33

回答

3

import android.telephony.gsm.SmsMessage;

以上應改爲:

import android.telephony.SmsMessage;

+0

和FYI,'telephony.gsm.SmsMessage'是用於當你不得不在CDMA和GSM手機之間指定的時候。現在你不需要指定'.gsm'或'.cdma',因爲Android變得更聰明瞭。 – Jakar 2012-04-06 03:11:37

相關問題