【问题标题】:Identify sms number ID in Oreo识别奥利奥中的短信号码ID
【发布时间】:2018-07-15 17:05:14
【问题描述】:

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

我已经阅读了几篇文章和线程,但它甚至没有得到意图,再次注意这段代码完全可以阻止呼叫,所以我将粘贴 SMS 相关信息

清单.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

我不想建一个app来管理短信,我只想截取号码ID做事。有人可以建议吗?

【问题讨论】:

    标签: java android broadcastreceiver


    【解决方案1】:

    你需要的是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

    【讨论】:

    • 这不是我要找的,但谢谢。我不想使用任何验证或其他任何东西,我认为您没有正确阅读我的问题。
    • 我现在又读了一遍。明天我会检查代码。我希望我能提供帮助
    猜你喜欢
    • 2018-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多