2017-05-16 67 views
1

我在webview中的應用程序中加載url但在webview下載功能時沒有發生什麼事情請點擊下載按鈕給我的解決方案。我已經在webview中加載url,但下載功能不能在webview中點擊下載按鈕

Public class FinancialActivity extends AppCompatActivity { 

WebView webview; 
public static final String SHARED_PREF_NAME = "myloginapp"; 
SharedPreferences sharedPreferences; 
String id, studid, degree, user_type; 
String url; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_financial); 

    sharedPreferences = FinancialActivity.this.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE); 
    studid = sharedPreferences.getString(LoginActivity.ID_SHARED_PREF, "Not Available"); 
    degree = sharedPreferences.getString(LoginActivity.KEY_DEGREE_SOUGHT, "Not Available"); 
    user_type = sharedPreferences.getString("user_type", "Not Available"); 

    url = Urlinfo.Financial + "stud_id=" + studid + "&user_type=" + user_type + "&subscription_status=" + DrawerActivity.subvalue + "&stud_degree=" + degree; 

    Log.d("urlapplication", "" + url); 

    WebView w = new WebView(this); 
    w.setWebViewClient(new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
    view.loadUrl(url); 
    return true; 
    } 
    }); 

    w.setDownloadListener(new DownloadListener() { 
    public void onDownloadStart(String url, String userAgent, 
    String contentDisposition, String mimetype, 
    long contentLength) { 
    Intent i = new Intent(Intent.ACTION_VIEW); 
    i.setData(Uri.parse(url)); 
    startActivity(i); 
    } 
    }); 

    setContentView(w); 
    w.loadUrl(url);  
} 
} 
} 
+0

? –

+0

是............. –

+0

檢查下面的答案 –

回答

0

這裏試試下面

@Override 
      public boolean shouldOverrideUrlLoading(WebView view, String url) { 
        // handle different requests for different type of files 
        // this example handles downloads requests for .apk and .mp3 files 
        // everything else the webview can handle normally 
        if (url.endsWith(".apk")) { //change .apk to any format you want to download 
         Uri source = Uri.parse(url); 
         // Make a new request pointing to the .apk url 
         DownloadManager.Request request = new DownloadManager.Request(source); 
         // appears the same in Notification bar while downloading 
         request.setDescription("Description for the DownloadManager Bar"); 
         request.setTitle("YourApp.apk"); 
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
          request.allowScanningByMediaScanner(); 
          request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
         } 
         // save the file in the "Downloads" folder of SDCARD 
         request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "SmartPigs.apk"); 
         // get download service and enqueue file 
         DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
         manager.enqueue(request); 
        } 
        else if(url.endsWith(".mp3")) { 
         // if the link points to an .mp3 resource do something else 
        } 
        // if there is a link to anything else than .apk or .mp3 load the URL in the webview 
        else view.loadUrl(url); 
        return true;     
      } 

希望這個代碼這有助於你想從它在網頁視圖打開的URL中下載文件