1

When closing an application with back or homescreen button, app showing a webview closes. On returning back to the app, it load the page all over again.

More specifically, the page this app loads contains an upload button. File upload takes some time depending on the internet speed. If the used starts uploading and goes to other app, the upload progress will be lost and on revisiting the app, the page will load all over again.

What to do to make upload in VebView work in background. Giving a notification of "upload in progress..." in notification area will be an added benefit. Suggest what to do and how?

Termininja
  • 6,620
  • 12
  • 48
  • 49
Chirag
  • 1,189
  • 2
  • 21
  • 43

2 Answers2

2

You should do the upload in a Service, instead of in a Activity. (Lookup IntentService for example, which will shut itself down after upload)

RvdK
  • 19,580
  • 4
  • 64
  • 107
2

enter image description here

here is sample code of service just edit is as per your need: i have create one service and call it from my activity using:

startService(new Intent(MainActivity.this, TempServices.class));

here is my service class

package com.example.uploadfile;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.util.Log;

public class TempServices extends Service {

    protected static final int ID = 100;
    private NotificationManager mNotifyManager;
    private Builder mBuilder;

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub

        mNotifyManager = (NotificationManager) getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle("Picture Download")
                .setContentText("Download in progress")
                .setSmallIcon(R.drawable.ic_launcher);
        // Start a lengthy operation in a background thread
        new Thread(new Runnable() {
            @Override
            public void run() {
                int incr;
                // Do the "lengthy" operation 20 times
                for (incr = 0; incr <= 100; incr += 5) {
                    // Sets the progress indicator to a max value, the
                    // current completion percentage, and "determinate"
                    // state
                    mBuilder.setProgress(100, incr, false);
                    // Displays the progress bar for the first time.
                    mNotifyManager.notify(0, mBuilder.build());
                    // Sleeps the thread, simulating an operation
                    // that takes time
                    try {
                        // Sleep for 5 seconds
                        Thread.sleep(5 * 1000);
                    } catch (InterruptedException e) {
                        Log.e("error-->", "sleep failure");
                    }
                }
                // When the loop is finished, updates the notification
                mBuilder.setContentText("Download complete")
                // Removes the progress bar
                        .setProgress(0, 0, false);
                mNotifyManager.notify(ID, mBuilder.build());
            }
        }
        // Starts the thread by calling the run() method in its Runnable
        ).start();
        return super.onStartCommand(intent, flags, startId);
    }

}

for counting progress check this link

do not forgate to add this service in android manifes:

<service android:name="TempServices" >
Community
  • 1
  • 1
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • I simply want to make it work in background like most of the others app out there. I don't need progress bar. The upload completion notice in my case is handled by javascript in the url I am loading... – Chirag Mar 30 '13 at 11:27
  • 1
    then remove mNotifyManager code. just use thread or AsyncTask or just method in service. – Dhaval Parmar Mar 30 '13 at 11:32