3

We are beginning with Android. We want to programmatically send files when internet is available (by email). We guess the best way to program it is to send the files to a queue if there isn't internet. When internet is detected, the files will begin to be uploaded one by one. We guess the code should be like the following:

A background service continually checks if there is internet:

public class EducarCabezoService extends Service{
    @Override
    public void onCreate() {
        super.onCreate();                 
        IntentFilter filter = new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);        
        ciReceiver = new checkInternetReceiver();
        registerReceiver(ciReceiver, filter);         

The background service uses a BroadcastReceiver for that purpose:

public class checkInternetReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {     
        Intent uqofIntent = new Intent(context, UploadQueueOfFilesService.class);
        context.startService(uqofIntent);

We didn't find how to put files in a queue (maybe it is not the normal procedure). We can use the method isOnline() from here:

public class MainActivity extends Activity {
    public uploadFile (){
        if (!isOnline()){
            // TODO Send files to queue?

We neither know how to upload those files from that queue:

public class UploadQueueOfFilesService extends Service{
    @Override
    public void onCreate() {
        // TODO Start uploading files
Community
  • 1
  • 1
chelder
  • 3,819
  • 6
  • 56
  • 90
  • 1
    This sounds like a good use case for [Square's Tape](http://square.github.io/tape/). – CommonsWare Jan 21 '14 at 20:22
  • Save the files to storage. An "out" folder for example. If there's files in there, upload. Here's an option for internal and external storage: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal – MikeHelland Jan 21 '14 at 20:26
  • You should be monitoring [CONNECTIVITY_ACTION](http://developer.android.com/reference/android/net/ConnectivityManager.html#CONNECTIVITY_ACTION) instead of SCAN_RESULTS_AVAILABLE_ACTION. See [this article](http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html#MonitorChanges) about monitoring connectivity status. – molnarm Jan 21 '14 at 20:33

1 Answers1

-1

This will save a file (to "internal storage", you can also use external storage (if their device has it)):

String FILENAME = "out/file1";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

When you go online, just look for files in the out directory. Upload and delete them one by one.

EDIT: Alternatively, if the files are already in storage, and you don't want to copy or delete them, use a SQLite table to store a row for every file your service should upload, and then delete the row from the table.

MikeHelland
  • 1,151
  • 1
  • 7
  • 17