How to queue data for sending a server on Android

I am working on an Android app with email function. I want my users to be able to compose and send emails in flight mode. To do this, I need some kind of queue that can check if there is a network and send, etc. I think this should be done 100 times. But I'm not quite sure why my searches do not go up very much. Does anyone know a git library or project that I can use for this? If not, does anyone know how to do this?

I believe it is called Queue and send pattern .

Update

I am starting a bounty on this. I hope this is a working example that does not use SMS. In my specific case, I am working on an Appengine Connected Android project. The client must send data to the server (String, Bitmap, etc. Under a specific POJO say Dog). I want you to be able to somehow interfere with this data. I can use Gson to save data to a file, etc. The bottom line is that I need to be able to check the network. When there is a network, I deactivate the turn to the server. If there is no network, I keep it in the queue.

My turn could be Queue<Dog> , where Dog is my class with fields such as Bitmap (or image path), String , long , etc.

I am looking for a working example. It may be very simple, but the example should work. A git zip will be great. I give almost half my points for this question.

 class Dog{ String dogname; String pathToImage; int dogAge; //etc. } //Design pattern for sending Dog to server 0) Unmarshall queue from file using Gson 1) Add dog to queue 2) If there is network, loop through queue and send data to server 3) if there is no network save queue to file //Ideally, as soon as there is network, the method should be able to detect so and run to send data to server 
+7
java android networking client-server
source share
1 answer

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) { //here, check that the network connection is available. If yes, start your email service. If not, stop your email service. ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); if (info != null) { if (info.isConnected()) { //start service Intent intent = new Intent(this, ItemServiceManager.class); startService(intent); } else { //stop service Intent intent = new Intent(this, ItemServiceManager.class); stopService(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.

+10
source share

All Articles