First you need to configure the receiver to watch the Wi-Fi connection, to see when they have data, you can also check the normal 3g / 4g connections and make a broadcast receiver for this. todo this allows you to use a realistic receiver to change the status of the connection. put something like this in the manifest in the application tag
<receiver android:name=".NetworkChangeReceiver" > <intent-filter> <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
now we need to make the recipient that we just defined in the manifest
public class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) {
What this does is add a large thick antenna called NetworkChangeReceiver on land android, which is set to listen when android has something to say about changing the state of the data connection.
Now you need to create your ItemServiceManager.class , which should read from the database (it should also extend the Service ). He should select the oldest item in the database (send it by email, send it, upload to the server, whatever), and if the connection was successful, delete the item from the database and download the next oldest. If not, close the service and broadcast receiver.
If you have a connection and need to send more data to the user, then add it to the database, and then make sure the service is running. Perhaps tell him that you need to double-check the database (after a few seconds) before deciding to close it, because nothing exists.
Here's how you can turn off the broadcast receiver.
PackageManager packageManager = context.getPackageManager(); ComponentName componentName = new ComponentName(context, NetworkChangeReceiver.class); packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
When a new item should be downloaded, if there is no web connection, the email should be stored in the database, and the broadcast receiver should be recognized when the Internet is back so that it can know when to download it. You can run it this way.
PackageManager packageManager = context.getPackageManager(); ComponentName componentName = new ComponentName(context, NetworkChangeReceiver.class); packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP);
The thing is that you only care about broadcasting connections when you have something saved for download, but cannot download it due to lack of data connection. When you have nothing to download, do not waste time processing and the battery while supporting the receiver / service. And when you have an email waiting, then start your translator to find out when you have a data connection so you can start downloading.
I do not think that someone is going to write a complete working solution for you, I hope this will be more than enough to help you along the way.
Edit:
Another thing you can do is let the server allow the adoption of an array of your elements, so you can just load it all at once when you get a valid connection. Typically, you would do this if each element were decently small. But if you are uploading pictures or videos or something big, itβs best to do it in one go.