Andy Aldo
Andy Aldo

Reputation: 890

android - Service need internet connection

So, I have a service that is supposed to send data to my server via MQTT. This process of sending of data is supposed to happen every 10 minutes and must be done regardless of the state of my application (Paused/Stopped/Destroyed). So, the conclusion here is my service needs internet connection even when the phone is in idle state in order to function properly.

The approach that I have taken is: Using AlarmManager and a WakefulBroadcastReceiver to run Service described in the first paragraph.

The steps can be described as follows:

  1. Attempt to connect to MQTT broker
  2. Upon successful connection, do some logic and publish message to server
  3. Upon successful message publish, disconnect from broker and clean up resources

The above sub-routine is supposed to be executed every 10 minutes.

The problem now is, after 3-4 minutes of idle state, my service is unable to connect to the MQTT broker because there seems to be no internet connection. I checked the internet connection via:

ConnectivityManager connectivityManager 
      = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
Log.wtf(TAG, "internet: " + (activeNetworkInfo != null && activeNetworkInfo.isConnected());

How do I acquire internet access for my service? The only thing that I haven't tried is using evernote/android-job. But before that, I would like to gain some information because I suspect the only way that I can solve this is by somehow acquiring internet access every time my service run.

I have browsed quite a bit on this topic, but all I came across is answers pertaining to 'How to check internet connection in background service' which is not what I need. What I need is 'how to acquire internet connection when phone is idle for every time my service runs?'

Please ask me if you need code snippets, or other information that I failed to include above. Thanks in advance.

Upvotes: 1

Views: 2551

Answers (5)

DKIT
DKIT

Reputation: 3481

Use a Sync Adapter. It will sync your data in the background at opportune moments to prevent unnecessary battery drain and service charges.

Upvotes: 1

Nidhin
Nidhin

Reputation: 1838

Android syncadapter mechanism give an efficient way to transfering data.You can read more at sync adapter and Transfering data using sync adapter

Upvotes: 1

Francesc
Francesc

Reputation: 29280

You can't force the phone to be online, you are at the whim of the user. The best you can do is check for internet connectivity, but if connectivity is off, there is no way for you to turn it on.

Your best approach would be to use the JobScheduler or Google GCM network manager, see these: JobScheduler, GCM Network Manager

With these you can schedule your job to fire at periodic intervals, only when network is available. If you truly need 10 minute periods and network, you will have to inform the user to ensure network is ON at all times, but you will still have to account for network being off.

Upvotes: 0

Kaushal28
Kaushal28

Reputation: 5557

how to acquire internet connection when phone is idle for every time my service runs?

You cannot access mobile data on / off programmatically above android 4.4 .It have been made stopped for security reasons ,instead you can ask the user using a dialog to enable the mobile data and then if he enable you can do your task.

So every time you start your service, ask user to enable mobile data.

However You can connect to WiFi programmatically:

WifiManager wifiManager = (WifiManager)this.context.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(status);

And set status to true or false as per your need.

Upvotes: 1

Harsh Patel
Harsh Patel

Reputation: 1086

Test your application in below 23 API. Because above 23 your application may not access the internet data because of **

"DOZE MODE"

**

So to overcome from that problem user must have to allow your app to run in background.

This is happening because of battery optimization. So please test it on below 23 and let me know if that works or not.

Upvotes: 0

Related Questions