【问题标题】:Continue uploading in background在后台继续上传
【发布时间】:2013-03-30 09:33:04
【问题描述】:

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

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

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

【问题讨论】:

标签: android upload android-webview android-notifications


【解决方案1】:

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

【讨论】:

    【解决方案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);
        }
    
    }
    

    统计进度请查看this链接

    不要伪造在 android 清单中添加此服务:

    <service android:name="TempServices" >
    

    【讨论】:

    • 我只是想让它像大多数其他应用程序一样在后台运行。我不需要进度条。在我的情况下,上传完成通知由我正在加载的 url 中的 javascript 处理...
    • 然后删除 mNotifyManager 代码。只需使用线程或 AsyncTask 或仅使用服务中的方法。
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多