13

我按照教程在我的应用程序上接收 SMS 并阅读它以将 SMSbody 传递给 Toast。那是接收器类。

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" />

该应用程序启动正常,但它不响应接收短信它不做任何事情。

4

3 回答 3

30

这是我接收短信的实现。短信可能会分成很多条,注意它是如何处理的。还要检查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 中声明您的接收器,那么无论您的应用程序是否启动过,系统都可以使用您的接收器。由于 Android 1.6 收到的短信通知是作为有序广播发送的,因此您可以使用android:priority属性 of<intent-filter>告诉系统首先将短信发送到您的应用程序(您也可以调用 abortBroadcast() 以便其他应用程序不会收到短信,例如本机短信应用程序)。不要忘记广播接收器有大约 10 秒的时间来执行它的操作,否则它可能在完成工作之前被提前终止。

于 2012-07-11T15:48:06.633 回答
2

广播区分大小写。android.provider.Telephony.SMS_RECEIVED不使用android.provider.telephony.SMS_RECEIVED

另外,我也有一个类别集,但我不确定它是否是强制性的:

<intent-filter>
   <action android:name="android.provider.Telephony.SMS_RECEIVED" />
   <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
于 2012-07-11T15:39:29.983 回答
2

注意:在某些设备上,如果没有意图过滤器中的android:priority="100" ,您的代码将无法工作:

 <application
 <uses-permission android:name="android.permission.RECEIVE_SMS" />
 <uses-permission android:name="android.permission.READ_SMS" />
 ...
  <receiver
        android:name=".SMSReceiver">
        <intent-filter android:priority="100">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
  </receiver>
 ...
</application>

这里是 Java 代码:

public class SMSReceiver extends BroadcastReceiver {
public static final String ACTION ="android.provider.Telephony.SMS_RECEIVED";
private static final String SMS_SENDER="123456789";

@Override
public void onReceive(Context context, Intent intent) {
        if (intent != null && intent.getAction() != null &&
                ACTION.compareToIgnoreCase(intent.getAction()) == 0) {
            Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
            SmsMessage[] messages = new SmsMessage[pduArray.length];
            for (int i = 0; i < pduArray.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
            }
            // SMS Sender, example: 123456789
            String sms_from = messages[0].getDisplayOriginatingAddress(); 

            //Lets check if SMS sender is 123456789
            if (sms_from.equalsIgnoreCase(SMS_SENDER)) {
                StringBuilder bodyText = new StringBuilder();

                // If SMS has several parts, lets combine it :)
                for (int i = 0; i < messages.length; i++) {
                    bodyText.append(messages[i].getMessageBody());
                }
                //SMS Body
                String body = bodyText.toString();
                // Lets get SMS Code
                String code  = body.replaceAll("[^0-9]", ""); 
            }
      }
 }
于 2018-07-30T04:32:39.937 回答