1

使用返回或主屏幕按钮关闭应用程序时,显示 web 视图的应用程序将关闭。返回应用程序后,它会重新加载页面。

更具体地说,此应用加载的页面包含一个上传按钮。文件上传需要一些时间,具体取决于互联网速度。如果用户开始上传并转到其他应用程序,则上传进度将丢失,并且在重新访问该应用程序时,页面将重新加载。

如何使 VebView 中的上传在后台工作。在通知区域提供“正在上传...”的通知将是一个额外的好处。建议做什么和怎么做?

4

2 回答 2

2

您应该在 a 中进行上传Service,而不是在 a 中Activity。(例如查找IntentService,上传后会自行关闭)

于 2013-03-30T09:36:56.557 回答
2

在此处输入图像描述

这是根据您的需要编辑的服务示例代码:我创建了一项服务并使用以下方法从我的活动中调用它:

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 回答