继续在后台上传

时间:2013-03-30 09:33:04

标签: android upload android-webview android-notifications

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

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

如何在VebView中进行上传工作。在通知区域发出“正在上传...”的通知将是一个额外的好处。建议做什么以及如何做?

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

enter image description here

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

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);
    }

}

计算进度检查this链接

不要在android中添加此服务:

<service android:name="TempServices" >