2016-04-26 105 views
0

我開始使用這個庫,它顯示了一個很好的上傳進度條。 https://github.com/gotev/android-upload-serviceUploadServiceBroadcastReceiver在活動結束時關閉

繼我加了一個廣播接收器,不過,雖然上傳作爲服務運行和精美更新文檔:

enter image description here

如果用戶關閉其中的BroadcastReceiver被宣佈我的活動在上傳結束後放棄回覆的能力。我能做些什麼來保持對broadcastReceiver的引用?

注意:我嘗試將接收器移動到單獨的單例類,但是這並沒有解決問題。

的廣播接收器:

public final UploadServiceBroadcastReceiver uploadReceiver = 
     new UploadServiceBroadcastReceiver() { 

      // you can override this progress method if you want to get 
      // the completion progress in percent (0 to 100) 
      // or if you need to know exactly how many bytes have been transferred 
      // override the method below this one 
      @Override 
      public void onProgress(String uploadId, int progress) { 
       Log.i(TAG, "The progress of the upload with ID " 
         + uploadId + " is: " + progress); 
      } 

      @Override 
      public void onProgress(final String uploadId, 
            final long uploadedBytes, 
            final long totalBytes) { 
       Log.i(TAG, "Upload with ID " + uploadId + 
         " uploaded bytes: " + uploadedBytes 
         + ", total: " + totalBytes); 
      } 

      @Override 
      public void onError(String uploadId, Exception exception) { 
       Log.e(TAG, "Error in upload with ID: " + uploadId + ". " 
         + exception.getLocalizedMessage(), exception); 


      } 

      @Override 
      public void onCompleted(String uploadId, 
            int serverResponseCode, 
            byte[] serverResponseBody) { 
       // At this point, the serverResponseBody has been completely downloaded 
       // and is cached in memory, so no NetworkOnMainThread could happen here 
       Log.i(TAG, "Upload with ID " + uploadId 
         + " has been completed with HTTP " + serverResponseCode 
         + ". Response from server: " 
         + new String(serverResponseBody)); 

       String response = new String(serverResponseBody); 

       dialog.dismiss(); 


       //If your server responds with a JSON, you can parse it 
       //from serverResponseBody using a library 
       //such as org.json (embedded in Android) or Google's gson 
      } 

      @Override 
      public void onCancelled(String uploadId) { 
       Log.i(TAG, "Upload with ID " + uploadId 
         + " has been cancelled by the user"); 


      } 
     }; 

回答

0

您可以創建延伸UploadServiceBroadcastReceiver一個新的類(如MyReceiver),在其添加您的業務邏輯,然後把它註冊爲您清單廣播接收器(如圖所示下面),意圖過濾器com.yourcompany.yourapp.uploadservice.broadcast.status。這樣您就可以獨立於您的活動和服務來收聽活動。更改com.yourcompany.yourapp,不管你已經在初始設置設置爲UploadService.NAMESPACE:

在AndroidManifest.xml

<receiver android:name="MyReceiver"> 
    <intent-filter> 
     <action android:name="com.yourcompany.yourapp.uploadservice.broadcast.status" /> 
    </intent-filter> 
</receiver> 

MyReceiver.java

public class MyReceiver extends UploadServiceBroadcastReceiver { 
    @Override 
    public void onProgress(UploadInfo uploadInfo) { 
    // your code here 
    } 

    @Override 
    public void onCancelled(UploadInfo uploadInfo) { 
    // Context context = getReceiverContext(); 
    // your code here 
    } 

    @Override 
    public void onError(UploadInfo uploadInfo, Exception exception) { 
     // Context context = getReceiverContext(); 
     // your code here 
    } 

    @Override 
    public void onCompleted(UploadInfo uploadInfo, ServerResponse serverResponse) { 
    // Context context = getReceiverContext(); 
    // your code here 
    } 
} 

非常重要的!如果要使用UploadServiceBroadcastReceiver處理事件,則不要在任何地方調用setDelegate方法。

更多於:https://github.com/gotev/android-upload-service/wiki/Monitoring%20upload%20status