D
D
derSamara2015-11-04 11:36:47
Android
derSamara, 2015-11-04 11:36:47

How to make the application automatically send SMS messages to the server?

Good afternoon!
I am writing an application, the essence of which is to send incoming SMS from a specific recipient to the wiremock service.
I did it first with a button when you click on which it sends, like, everything is cool.
But I need the application to automatically send SMS if it comes (from a specific user).
To do this, I decided to use IntentService, but it still doesn’t work, and as the message came, it was sent 1 time and that’s it.
Can you tell me in what way it is possible or through which class it is possible to implement so that the application constantly analyzes the SMS message and if it arrives, then by default it immediately sends it ??
Thanks in advance for any help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Gamega, 2015-11-04
@derSamara

1. We listen to SMS by the receiver so that it is processed earlier than by the default application, we set the priority higher

<receiver android:name=".sms.SmsReceiver" >
            <intent-filter android:priority="1000" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

2. read the number and text of the SMS, if it matches the desired one, then send information to the server in a new stream
@Override
    public void onReceive(Context context, Intent intent) {
        if (intent != null
                && intent.getAction() != null
                && ACTION.compareToIgnoreCase(intent.getAction()) == 0) {

            Object[] pduArray = (Object[]) intent.getExtras().get("pdus");
            SmsMessage[] messages = new SmsMessage[pduArray.length];
    
            for (int i = 0; i < pduArray.length; i++) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    messages[i] = SmsMessage.createFromPdu(((byte[]) pduArray[i]), intent.getStringExtra(FORMAT));
                } else {
                    messages[i] = SmsMessage.createFromPdu((byte[]) pduArray[i]);
                }
                String phone =  messages[i].getOriginatingAddress();
                String message =  messages[i].getMessageBody();

}
}
}

3. if it is necessary that the message does not get anywhere further and does not distract the user
abortBroadcast();

B
belozerow, 2015-11-04
@belozerow

habrahabr.ru/post/149555

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question