在android webview中下载文件而不是浏览器

时间:2011-07-25 20:28:37

标签: android file webview downloading

如何在zip内下载apkWebView文件?因为每次我点击WebView内的任何链接,它首先打开浏览器我需要在没有浏览器的情况下下载文件。 我是Java的新手,我正在通过stackoverflow学习,这是我的代码,请有人为我提供正确的代码:

package com.webview;


import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HelloWebViewActivity extends Activity {
    private WebView webview;




    /** Called when the activity is first created. */



    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        webview.setWebViewClient(new HelloWebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setInitialScale(50); 
        webview.getSettings().setUseWideViewPort(true); 
        webview.setVerticalScrollBarEnabled(false);
        webview.setHorizontalScrollBarEnabled(false);
        webview.loadUrl("my URL");
        webview.setWebViewClient(new WebViewClient()

        {
            public void onLoadResource (WebView view, String url) 
            {

                Log.i("onLoadResource()", "url = " + url);
                if (url.endsWith(".apk")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    view.getContext().startActivity(intent);


                } 
                     else super.onLoadResource(view,url);
                }

        }
        );


    }









    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu); // Add menu items, second value is the id, use this in the onCreateOptionsMenu
        menu.add(0, 1, 0, "Apps");
        menu.add(0, 2, 0, "Games");
        menu.add(0, 3, 0, "Back");
        menu.add(0, 4, 0, "Favorites");
        menu.add(0, 6, 0, "Forward");
        return true; // End of menu configuration
    }
    public boolean onOptionsItemSelected(MenuItem item){ // Called when you tap a menu item
        switch (item.getItemId()){
            case 1: //If the ID equals 1, go back
                webview.loadUrl("?page_id=169");
            return true;
            case 2 : //If the ID equals 2, refresh
                webview.loadUrl("?page_id=153");
            return true;
            case 3: //If the ID equals 3, go forward
                webview.goBack();
            return true;
            case 4: //If the ID equals 1, go back
                webview.loadUrl("?page_id=187"); 
            return true;
            case 6: //If the ID equals 3, go forward
                webview.goForward();
            return true;
            }
        return false;
        }


    private class HelloWebViewClient extends WebViewClient {












    @Override
    public void onReceivedError(WebView view,int errorCode,String description,String failingUrl) {
       try {view.stopLoading();} catch(Exception e){}
       try {view.clearView();} catch(Exception e){}
       view.loadUrl("file:///android_asset/wifi.html");
    }
        }


 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

 @Override
 public void onConfigurationChanged(Configuration newConfig){        
     super.onConfigurationChanged(newConfig);
 }









}

0 个答案:

没有答案