0

I am making an Android app to forward SMS to Telegram. There is a broadcastReceiver listening to SMS, and then call a HTTP request sending to my own server. It works fine. However, when the screen is off, wifi is off as well. When there is a SMS, there is no network to send the HTTP request.

I have searched about Wakelock and Wifilock. Both of them seems not applicable in this case.

Is there a way to wake up the network connection for a while, to finish the HTTP call?

Thanks.

AndroidManifest.xml

<receiver android:name=".SmsReceiver" android:enabled="true" android:exported="true">
        <intent-filter android:priority="500">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

SmsReceiver.java

public class SmsReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
    WifiLockManager.acquireWifiLock(context);

    RequestQueue queue = Volley.newRequestQueue(context);
    String url = "https://smsforwarder.mydomain.xyz/";

    Bundle bundle = intent.getExtras();
    if (bundle != null) {
        String sender = "";
        String message = "";

        SmsMessage[] sms = Telephony.Sms.Intents.getMessagesFromIntent(intent);
        for (int i=0; i < sms.length; i++) {
            SmsMessage smsMessage = sms[i];
            sender = smsMessage.getOriginatingAddress();
            message += smsMessage.getMessageBody();
        }

        Map<String, String> postParam = new HashMap<String, String>();
        postParam.put("sender", sender);
        postParam.put("message", message);

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(
                Request.Method.POST,
                url,
                new JSONObject(postParam),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        Toast.makeText(context, "SMS forwarded", Toast.LENGTH_SHORT).show();
                        WifiLockManager.releaseWifiLock();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(context, "SMS forward failed", Toast.LENGTH_SHORT).show();
                WifiLockManager.releaseWifiLock();
            }
        }
        ){
            @Override
            public Map<String, String> getHeaders() {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
        };

        queue.add(jsonObjReq);
    }
}

}

ロジャー
  • 347
  • 1
  • 6
  • 16
  • I think my answer can help you : https://stackoverflow.com/q/50628102/8572503 – exploitr Jul 12 '18 at 09:06
  • This describes how you can make broadcast receiver run always. To send a specific broadcast, you can use the `LocalBroadcastManager` – exploitr Jul 12 '18 at 09:13
  • invoke your http call when network changed check broadcast receiver here: https://stackoverflow.com/a/26114247/5955362 – Jaydeep Devda Jul 12 '18 at 09:16
  • @JaydeepPatel I want to send out the HTTP request ASAP so I can get the forwarded message in Telegram soon. – ロジャー Jul 12 '18 at 09:46
  • @Toaster Since my phone gets the SMS and vibrates, I think the broadcast receiver is working. It's just no wifi network so no HTTP call is possible. – ロジャー Jul 12 '18 at 09:58
  • @y2kbug Please allow me to understand some of your procedure. Post some code, how you're doing ? → "what's not working" – exploitr Jul 12 '18 at 10:00
  • @Toaster I have updated the post. Please ignore the WifiLock since it is useless. My problem now is that, I have installed the app on my phone, and put it aside, when there is a SMS received, my phone vibrates, but nothing is forwarded out. The code above works, but if the network is asleep, it is not. – ロジャー Jul 12 '18 at 10:06

1 Answers1

0

CAUTION: Do not do network request in the broadcast receiver onReceive, because it will trigger and terminates for some time i.e for only 8 to 10 seconds. If you want to forcefully do it in onReceive means you Should use goAsync method in receiver. Or you can call any service or work manager like background service.

san
  • 91
  • 4