2013-03-30 138 views
1

使用后退或主屏幕按钮关闭应用程序时,显示web视图的应用程序关闭。 在返回到应用程序时,它会重新加载页面。继续在后台上传

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

如何使VebView上传在后台工作。在通知区域发出“正在上传...”的通知将会带来额外的好处。建议做什么和如何?

+2

http://stackoverflow.com/a/12179687/1168654 –

+0

@DhavalSodhaParmar我想'AsyncTask'是我应该去的是因为'Service'无法加载用户界面(在我的情况下是WebView)。感谢分享! – Chirag

+1

是的,但你可以使用服务。检查此:http://android-er.blogspot.in/2011/04/start-service-to-send-notification.html –

回答

2

您应该在Service而不是Activity上执行上传操作。 (查找IntentService例如,上载后,将自行关闭)

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链接

不forgate在安卓manifes加入此服务:

<service android:name="TempServices" > 
+0

我只是想使它工作在背景像大多数其他应用程序在那里。我不需要进度条。在我的情况下,上传完成通知由我在加载的url中的javascript处理... – Chirag

+1

然后删除mNotifyManager代码。只是使用线程或AsyncTask或只是在服务方法。 –