【问题标题】:broadcast service for incoming sms dosent work传入短信的广播服务不起作用
【发布时间】:2019-07-15 09:31:52
【问题描述】:

我将以下类用于我的 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>

【问题讨论】:

  • 我猜不再需要权限了。您是否注意到消息应用程序要求消息权限但仍显示 OTP。这就是我猜proandroiddev.com/…

标签: android broadcastreceiver sms smsmanager


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 1970-01-01
    • 2023-01-23
    • 2022-11-28
    • 2012-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多