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?

4

2 に答える 2

2

Serviceではなく、 でアップロードを行う必要がありますActivityIntentService(たとえば、アップロード後に自動的にシャットダウンするルックアップ)

于 2013-03-30T09:36:56.557 に答える
2

ここに画像の説明を入力

これがサービスのサンプルコードです。編集は必要に応じて行います。1つのサービスを作成し、次を使用してアクティビティから呼び出します。

startService(new Intent(MainActivity.this, TempServices.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);
    }

}

進行状況をカウントするには、このリンクを確認してください

このサービスを Android マニフェストに追加することを忘れないでください。

<service android:name="TempServices" >
于 2013-03-30T11:17:06.590 に答える