我如何在Oreo上收到短信?

时间:2019-05-12 11:40:03

标签: java android broadcastreceiver sms

我希望我的手机(三星A5,Android 8.0.0 Oreo)通过发送短信与其他设备(Arduino)通信。 我设法发送了邮件,但以某种方式无法接收SMS。

我看过许多(约12个)教程,但它们要么是针对较旧的android的,要么是显式地产生错误,或者就是不起作用。这是我发现的2种方法:

我有这个课,我在这里参考主要活动:

// imports are OK
class SMS {

    private Listener mListener;
    private final Activity activity;
    private final Context context;

    SMS(Activity activityArg){
        activity = activityArg;
        context = activity.getApplicationContext();
        checkForPermission(activity); // checked, working

// method 1
        final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
        final String SMS_SENDER = "123456789";
        context.registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context context, Intent intent) {
                if ((intent != null) &&
                        (intent.getAction() != null) &&
                        (intent.getExtras() != null) &&
                        (ACTION.compareToIgnoreCase(intent.getAction()) == 0)) {
                    Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
                    if (pduArray != null) {
                        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 (SmsMessage message : messages) {
                                bodyText.append(message.getMessageBody());
                            }
                            //SMS Body
                            String body = bodyText.toString();
                            //Send sms to activity via listener
                            mListener.recieveCallback(body);
                            // Lets get SMS Code
                            String code = body.replaceAll("[^0-9]", "");
                        }
                    }
                }
            }
        }, new IntentFilter("SMS_Recieved"));
    }

    public class SmsReceiver extends BroadcastReceiver {
// method 2
        static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
        private static final String SMS_SENDER = "123456789"; //actually this is other number, just for clarification

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

                    String sms_from = messages[0].getDisplayOriginatingAddress();

                    //Lets check if SMS sender is from number i want
                    if (sms_from.equalsIgnoreCase(SMS_SENDER)) {
                        StringBuilder bodyText = new StringBuilder();

                        for (SmsMessage message : messages) {
                            bodyText.append(message.getMessageBody());
                        }
                        //SMS Body
                        String body = bodyText.toString();
                        //Send sms to activity via listener
                        mListener.recieveCallback(body);
                        // Lets get SMS Code
                        String code = body.replaceAll("[^0-9]", "");
                    }
                }
            }
        }
    }

// sends received sms back to activity - works
    public interface Listener {
        void recieveCallback(String str);
    }

    void setListener(Listener listener) {
        mListener = listener;
    }
}


我也有这样的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.maneren.smartcollar">
    <uses-feature android:name="android.hardware.usb.host" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <application
        android:allowBackup="true"
        android:fullBackupContent="true"
        android:icon="@mipmap/launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/launcher"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
<!-- to method 2 and 3 -->
        <receiver
            android:name=".SMS$SmsReceiver">
            <intent-filter android:priority="100">
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".MainActivity"
            android:screenOrientation="sensorPortrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>
    </application>

</manifest>

当我收到短信时,什么也没发生(用另一部手机而不是arduino进行测试,其电话号码不受限制)

0 个答案:

没有答案