E
E
Eugene2014-07-31 13:19:47
Java
Eugene, 2014-07-31 13:19:47

BroadcastReceiver + IntentService, how to send an http request while the device is idle (sleep)?

I want to make an application for androyd, which, when receiving SMS, sends an http request to my server. Actually, I wrote the application, it even works, but with reservations.
Namely, the http request is correctly sent only when the phone is turned on and in active mode. If SMS is received when the phone is in sleep mode, then the request is not sent, although the receiver is triggered.
I noticed a rather interesting behavior - if you connect the debug and press the device's power button (put it to sleep), then the HTTP request will still be sent. But, as soon as you turn off the cord, the application stops sending requests.
I have already tried many ways - I made a service that creates a new receiver instance on onCreate

@Override
    public void onCreate(){
        super.onCreate();
        _SMSReceiver = new SMSReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(_SMSReceiver, filter);
        Log.e("Connect", "onStart");
    }

Then I made an IntentService, an instance of which is created when the receiver fires
@Override
protected void onHandleIntent(Intent intent)
{
..............
..............
          HttpAsyncSendOperation http = new HttpAsyncSendOperation(this, smsDTO);
          http.execute();
}

There is only one result: the http request is not sent if the phone is in sleep mode, but the receiver always works.
How to solve the problem, maybe I'm using the android services functionality incorrectly?
PS
Sending an Http request I wrap in asynctask;
Here is the method called from doInBackground
Request submission method
public static String SendHTTPRequest(String URI, List<NameValuePair> params)
{
    String paramsString = URLEncodedUtils.format(params, "UTF-8");	    
      
      HttpParams httpParameters = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(httpParameters, 8000);
      HttpConnectionParams.setSoTimeout(httpParameters, 15000);
      
      HttpClient httpclient = new DefaultHttpClient(httpParameters);
      HttpGet httpget = new HttpGet(Preferences.Host_URL + "?" + paramsString);
      try
      {	    		    	
      	HttpResponse response = httpclient.execute(httpget);
          HttpEntity entity = response.getEntity();

          if (entity != null)
          {
              InputStream instream = entity.getContent();
              String result = inputStreamToString(instream);

              instream.close();	            
              return result;
          }
          else return "";
      }
      catch (Exception e)
      {
      	return null;
      }        
}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2014-08-04
@pushthebutton

The issue was resolved.
On my android version 2.3.6, the services really couldn't communicate with the network for packet data. It does not matter which network it is (GSM or WIFI).
When installing the application on androyd 4.1 (device from samsung), there are no problems at all - data is sent both in standby mode and in working mode.
I also tested it on a device with kitkat (alcatel), the result is predictable - everything is normal.
There is a problem in the version of androyd 2.3.6 or in a specific firmware from the manufacturer (Samsung Galaxy 1)

A
Alexey Kulakov, 2014-07-31
@carbon88

maybe in androyd there is some option that prohibits network interactions in the background if the phone is running on battery power, some kind of saving mode? but this is purely a guess because
1) active mode and on battery - works
2) active mode from the cord (charging) - works
3) background mode and from the battery - does not work
4) background mode from the cord - works
as a solution to the problem queue of incoming messages and send them in active mode. because it is not known how the application will work on other phones with other energy efficiency optimizers.
it is not known what energy efficiency mode the manufacturer has washed down and what is prohibited there

M
Marat S, 2014-08-01
@aratj

Or maybe just in the settings you have, so that the data transfer is cut down when the phone. not used ?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question