广播短信服务

时间:2019-07-15 09:31:52

标签: android broadcastreceiver sms smsmanager

我将以下课程用于我的SMS接收器。我不知道这是怎么回事。我听说Google在2018年以后不再允许SMS权限。只允许读取带有#标签的SMS,但不幸的是,我也没有收到这些SMS。

JAVA

public class IncomingSms extends BroadcastReceiver {
    final SmsManager sms = SmsManager.getDefault();

    @Override
    public void onReceive(Context context, Intent intent) {

        final Bundle bundle = intent.getExtras();
        try {
            if (bundle != null) {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj.length; i++) {
                    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();
                    Toast.makeText(context, senderNum, Toast.LENGTH_LONG).show();
                    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
                } // end for loop
            } // bundle is null
        } catch (Exception e) {
        }

    }
}

如您所见,我添加了与SMS相关的所有权限,并定义了自己的优先级为“ 999”的接收者

AndroidManifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<!-- alarm manager -->
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_intheater" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />


    <activity android:name=".ui.login.Login">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".MainActivity"/>
    <activity android:name=".ui.verification.VerificationActivity"></activity>

    <receiver
        android:name=".service.IncomingSms"
        android:permission="android.permission.BROADCAST_SMS"
        android:exported="true"
        android:enabled="true">
        <intent-filter
            android:priority="999">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>
    <receiver android:name=".service.BootUpService">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>
    <receiver android:name=".service.AutoStart">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
        </intent-filter>
    </receiver>
    <service
        android:name=".service.AlarmService"
        android:enabled="true"
        >
    </service>
    <service
        android:name=".service.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT" />
        </intent-filter>
    </service>
    <receiver android:process=":remote" android:name=".service.Alarm"></receiver>

</application>

1 个答案:

答案 0 :(得分:0)

我发现我没有从设置->应用程序->应用程序名称->权限中检查权限

许多用户不接受接收和读取短信权限。为了获得更好的用户体验和安全性,最好使用https://developers.google.com/identity/sms-retriever/user-consent/overviewhttps://developers.google.com/identity/sms-retriever/overview。 这两种方法不需要用户的短信权限,而仅需要访问服务器发送的短信