gpt4 book ai didi

java - 在 Oreo 中识别短信号码 ID

转载 作者:太空宇宙 更新时间:2023-11-04 10:19:09 25 4
gpt4 key购买 nike

我的实际代码完美地阻止了调用,但现在我想识别传入的 SMS 号码 ID 并执行一些操作,例如标记为已读或其他操作(例如 Mediumthis 之一)。

我已经阅读了几篇文章和线程,但它甚至没有理解 Intent ,再次注意,此代码可以完美地阻止调用,因此我将粘贴 SMS 相关信息

Manifest.xml

<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

<service android:name=".CallReceiverService" />

使用广播接收器的服务

@Override
public void onCreate() {
super.onCreate();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, 0);

Notification notification = new Notification.Builder(this, SERVICE_CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(this.getResources().getString(R.string.stg_ServiceRunning))
.setContentIntent(pendingIntent)
.setCategory(Notification.CATEGORY_CALL)
.build();

startForeground(44332255, notification);
}

IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("android.intent.action.PHONE_STATE"); // related to call feature, ignore
intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
intentFilter.addAction("Telephony.Sms.Intents.SMS_RECEIVED_ACTION");
intentFilter.setPriority(1000);
registerReceiver(callCheckReceiver, intentFilter);
}


private BroadcastReceiver callCheckReceiver = new BroadcastReceiver() {

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

try {
if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
Log.d("Call", "SMS received");
String smsSender = "";
if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
Log.d("Call", "SMS received");
String smsSender = "";
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
smsSender = smsMessage.getDisplayOriginatingAddress();
}

if (!isValidPhoneNumber(smsSender)) {
Log.d("Call", "Invalid SMS detected: From " + smsSender);
}
}
if (!isValidPhoneNumber(smsSender)) {
Log.d("Call", "Invalid SMS detected: From " + smsSender);
}
}

} catch (Exception e) {
e.printStackTrace();
}
}

};

public static boolean isValidPhoneNumber(String phoneNumber) {
return android.util.Patterns.PHONE.matcher(phoneNumber).matches();
}

基本上,我在 MainActivity 中请求许可,在 Manifest 中设置它们,并在 Oreo 或更低版本的 Android 中正确调用的 Service 中传递 FilterIntent。目标 API >=19

我不想构建一个应用程序来管理短信,我只是想拦截号码ID并做一些事情。有人可以建议吗?

最佳答案

您需要的是SMS Retriever API

如果你想检测短信,你可以简单地使用

    SmsRetrieverClient client = SmsRetriever.getClient(this /* context */);
Task<Void> task = client.startSmsRetriever();
task.addOnSuccessListener(new OnSuccessListener<Void>()
{
@Override
public void onSuccess(Void aVoid)
{
// Successfully started retriever, expect broadcast intent
// ...
}
});

task.addOnFailureListener(new OnFailureListener()
{
@Override
public void onFailure(@NonNull Exception e)
{
// Failed to start retriever, inspect Exception for more details
// ...
}
});

在 AndroidManifest.xml 中只需添加接收器

    <receiver
android:name=".custom.SMSBroadcastReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED" />
</intent-filter>
</receiver>

在接收器中,您可以对检测到的消息执行任何操作

    public class SMSBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (SmsRetriever.SMS_RETRIEVED_ACTION.equals(intent.getAction()))
{
Bundle extras = intent.getExtras();
Status status = (Status) extras.get(SmsRetriever.EXTRA_STATUS);
switch (status.getStatusCode())
{
case CommonStatusCodes.SUCCESS:
// Get SMS message contents
String message = (String) extras.get(SmsRetriever.EXTRA_SMS_MESSAGE);
// Extract one-time code from the message and complete verification
// by sending the code back to your server for SMS authenticity.




break;
case CommonStatusCodes.TIMEOUT:
// Waiting for SMS timed out (5 minutes)
// Handle the error ...
break;

}
}
}
}

需要注意的是,SMSRetrieverClient 默认超时时间为 5 分钟。

要创建可检测的短信,请遵循 SMS Creator for Google

关于java - 在 Oreo 中识别短信号码 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51350406/

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