gpt4 book ai didi

android - 如何修复未 protected SMS BroadcastReceiver lint 警告

转载 作者:可可西里 更新时间:2023-11-01 18:58:13 26 4
gpt4 key购买 nike

我的应用需要能够接收短信。一切正常,但我收到此 lint 警告:

BroadcastReceivers that declare an intent-filter for SMS_DELIVER or SMS_RECEIVED must ensure that the caller has the BROADCAST_SMS permission, otherwise it is possible for malicious actors to spoof intents.

如何“确保调用方具有 BROADCAST_SMS 权限”?

在我的 list 中我有:

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application ...>
<receiver
android:name=".SmsReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="1000">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>

我的代码:

public class SmsReceiver extends BroadcastReceiver {
public SmsReceiver() {}

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

final Bundle bundle = intent.getExtras();
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
final SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
// use currentMessage
}
}
}
}

最佳答案

添加android:permission="android.permission.BROADCAST_SMS"到开幕<receiver>标签。例如:

<receiver
android:name=".SmsReceiver"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">

android:permission <receiver> 上的属性指定广播的发送者 必须持有哪个权限才能广播到您的 <receiver> .这是一种安全措施;在这种情况下,您可以相对确定是系统发送了 SMS_RECEIVED播送。它不是严格要求的,但很明显,如果它不存在,lint 会提示。

关于android - 如何修复未 protected SMS BroadcastReceiver lint 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36127486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com