2013-10-24 42 views
0

我已經使用通知來顯示下載文件的狀態。我只是簡單地使用FileOutputStream來下載文件,以便它可以在小於9的API上工作。但現在我的問題是在完成下載之後,我試圖在相應的閱讀器中打開該文件。但它會拋出窗口異常泄露如果我關閉了應用程序,在下載文件之間。通知完成後打開下載文件

我的進度更新方法代碼如下,請幫我出來。

@Override 
protected void onProgressUpdate(Integer... values) { 
    super.onProgressUpdate(values); 
    progressUpdate(values[0]); 
} 

@SuppressWarnings("deprecation") 
public void progressUpdate(int percentageComplete) { 
    CharSequence contentText = percentageComplete + "% complete"; 
    mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, 
      mContentIntent);    
    mNotificationManager.notify(NOTIFICATION_ID, mNotification); 

    if(percentageComplete==100){ 

     try { 
      Log.e("file path ",file.getAbsolutePath()); 
      Uri path = Uri.fromFile(file); 
      notificationIntent.setDataAndType(path, "application/pdf"); 
      notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      startActivity(notificationIntent); 
      finish(); 

     } catch (ActivityNotFoundException e) { 
      Toast.makeText(getApplicationContext(), 
        "Unable to open file ,PDF Reader application is not installed in your device", Toast.LENGTH_SHORT).show(); 
     } 

    } 
} 
+0

當活動完成時,Android發出了windowlink錯誤,並且您希望打開對話框或任何視圖提供程序想要附加到窗口。 – nurisezgin

回答

1

當你的應用程序在後臺或關閉後,你不應該顯示吐司/對話框。您可以從Activity生命週期的onPause()方法處理此操作,取一個布爾值並在onPause()上將其設置爲true。如果它的真實意思是你的應用程序在後臺,你不應該顯示對話框。