Testing Android Android Receiver

I am trying to write a unit test in BroadcastReceiver that receives information when an SMS is received. This is not intended for the default application. Instead, I just need this for two-factor authentication. For this case, I created a PDU with [1].

But when I transmit it to BroadcastReceiver, the sender's nr phone will never be read by android, it will just be zero. Body text is returned.

@TargetApi(Build.VERSION_CODES.KITKAT) @Test public void testOnReceive() throws Exception { final byte[] decodedPDU = BaseEncoding.base16().decode(PDU); final ReceiveSmsBroadcastReceiver receiveSmsBroadcastReceiver = spy(new ReceiveSmsBroadcastReceiver(true)); final Intent intent = new Intent(); intent.putExtra("format",SmsConstants.FORMAT_3GPP); intent.putExtra("pdus", new Object[]{decodedPDU}); intent.setAction("android.provider.Telephony.SMS_RECEIVED"); intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, 1); receiveSmsBroadcastReceiver.onReceive(InstrumentationRegistry.getTargetContext(), intent); 

In the receiver, I do this to get SMSMessage objects:

 @TargetApi(Build.VERSION_CODES.KITKAT) private void getSMSKitKat(final Context context, final Intent intent) { final SmsMessage[] messagesFromIntent = Telephony.Sms.Intents.getMessagesFromIntent(intent); 

I get an SmsMessage array here, the body message is correct. But I need to check my verification sender's phone number before I can notify the user interface that the received SMS: But nr is always null here:

 private boolean isCorrectSender(@Nullable final SmsMessage message) { if (message == null) { return false; } final String nr = message.getOriginatingAddress(); 

Can someone tell me what is wrong here?

PS: SMSConstants and PhoneConstants are all the infrastructure classes that I took with AOSP to run it, because these APIs are not public.

[1] http://twit88.com/home/utility/sms-pdu-encode-decode

+5
source share

Source: https://habr.com/ru/post/1234886/


All Articles