2012-07-11 42 views
7

我按照教程在我的應用程序上接收短信,並閱讀它以將SMSbody傳遞給Toast。 即Receiver類。在Android應用程序上接收短信

public class SmsReciever 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+= msgs[i].getMessageBody(); 

      } 
      Toast.makeText(context, str, Toast.LENGTH_LONG).show(); 
     } 
     } 

    } 

清單文件

<receiver android:name="com.msoft.masrooq.SmsReciever"> 
    <intent-filter> 
    <action android:name="android.provider.telephony.SMS_RECIEVED"></action> 
    </intent-filter> 
    </receiver> 
     <uses-permission android:name="android.permission.RECEIVE_SMS"/> 
      <uses-permission android:name="android.permission.READ_SMS" /> 

應用程序啓動正常,但響應不接收短信 它不會做任何事情。

+0

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

+0

檢查這個簡單的教程http://tech-papers.org/receive_sms_from_android_app/ – 2015-01-18 10:42:14

回答

19

這是我的接收短信的實現。短信可能會分解成許多,注意它是如何處理的。同時檢查android:priority屬性。

public class SmsReceiver extends BroadcastReceiver { 

    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     if (intent.getAction().equals(SMS_RECEIVED)) { 
      Bundle bundle = intent.getExtras(); 
      if (bundle != null) { 
       // get sms objects 
       Object[] pdus = (Object[]) bundle.get("pdus"); 
       if (pdus.length == 0) { 
        return; 
       } 
       // large message might be broken into many 
       SmsMessage[] messages = new SmsMessage[pdus.length]; 
       StringBuilder sb = new StringBuilder(); 
       for (int i = 0; i < pdus.length; i++) { 
        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); 
        sb.append(messages[i].getMessageBody()); 
       } 
       String sender = messages[0].getOriginatingAddress(); 
       String message = sb.toString(); 
       Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 
       // prevent any other broadcast receivers from receiving broadcast 
       // abortBroadcast(); 
      } 
     } 
    } 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.smsreceiver" 
    android:versionCode="1" 
    android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="4" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" /> 
    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity 
      android:name=".SmsLoggerActivity" 
      android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <receiver android:name="com.example.smsreceiver.SmsReceiver" android:enabled="true"> 
      <intent-filter android:priority="2147483647"> 
       <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 

幾點注意事項: 如果你宣佈你的接收機XML比系統可以使用你的接收機,無論你的應用是有史以來推出。 由於有關接收到的SMS消息的Android 1.6通知以有序廣播的形式發送,因此您可以使用android:priority屬性<intent-filter>來告訴系統首先將SMS發送給您的應用程序(您也可以調用abortBroadcast(),以便其他應用程序不會收到短信,例如本地短信應用程序)。不要忘記廣播接收機在執行操作時大約需要10秒鐘,否則在完成作業之前可能會提前終止廣播接收機。

+0

+1剛剛起來,android。權限.SEND_SMS和android.permission.RECEIVE_SMS不同。在你的主要活動中,只需創建一個對象SmsReceiver()。 – Sydwell 2014-03-31 13:28:52

+0

如何爲MMS實現這一點? – Arya 2018-02-28 22:43:32

2

廣播區分大小寫。使用 「android.provider 電話 .SMS_RECEIVED。」 而不是 「android.provider 電話 .SMS_RECEIVED。」

而且,我也有一類集,但我不知道它是強制性的:

<intent-filter> 
    <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
    <category android:name="android.intent.category.DEFAULT" /> 
</intent-filter> 
相關問題