Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android webview中下载文件,而不是在浏览器中下载_Android_File_Webview_Download - Fatal编程技术网

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

在android webview中下载文件,而不是在浏览器中下载,android,file,webview,download,Android,File,Webview,Download,如何在WebView中下载zip或apk文件?因为每次我点击WebView中的任何链接,第一次打开浏览器时,我需要下载一个没有浏览器的文件。 我是Java新手,我正在通过stackoverflow学习,这是我的代码,请有人给我提供正确的代码: package com.webview; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.app.Activ

如何在
WebView
中下载
zip
apk
文件?因为每次我点击
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);
 }









}