如何在我的Web视图应用程序中启用下载。这是我的代码

时间:2016-01-13 19:15:09

标签: java android android-studio

现在我有一些用户可以使用webview从网站上下载的文件,当我点击该文件时,他们没有响应并且下载没有开始。 同样,我无法使用webview上传文件。

如何在webview上启用下载,以便我可以通过此应用程序下载我的文件。

MyAppWebViewClient.java

 package xyz.hamsterwheelengineer.www.minutenotes;    

 import android.content.Intent;
 import android.net.Uri;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import android.webkit.DownloadListener;


 public class MyAppWebViewClient extends WebViewClient {
 @Override
 public boolean shouldOverrideUrlLoading(WebView view, String url) {

 if(Uri.parse(url).getHost().endsWith(("hamsterwheelengineer.xyz"))) {
 return false;
    }

    if(Uri.parse(url).getHost().endsWith(("google.com"))) {
        return false;
    }


    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    view.getContext().startActivity(intent);
    return true;
  }
 }

MainActivity.java

package xyz.hamsterwheelengineer.www.minutenotes;

import android.net.Uri;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ShareActionProvider;
import android.webkit.DownloadListener;

public class MainActivity extends Activity {

private WebView mWebView;

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

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://hamsterwheelengineer.xyz/");
    mWebView.setWebViewClient(new xyz.hamsterwheelengineer.www.minutenotes.MyAppWebViewClient(){
        @Override
        public void onPageFinished(WebView view, String url) {
            //hide loading image
            findViewById(R.id.progressBar1).setVisibility(View.GONE);
            //show webview
            findViewById(R.id.activity_main_webview).setVisibility(View.VISIBLE);
        }});


}



@Override
public void onBackPressed() {
    if(mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}


private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    /** Inflating the current activity's menu with res/menu/items.xml */
    getMenuInflater().inflate(R.menu.menu_main, menu);

    /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share).getActionProvider();

    /** Setting a share intent */
    mShareActionProvider.setShareIntent(getDefaultShareIntent());

    return super.onCreateOptionsMenu(menu);

}

/** Returns a share intent */
private Intent getDefaultShareIntent(){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_SUBJECT, "minuteNotes - Get all of your engineering notes at one place");
    intent.putExtra(Intent.EXTRA_TEXT," Visit www.hamsterwheelengineer.xyz or download our minuteNotes app from the playstore to begin.");
    return intent;
}


}

1 个答案:

答案 0 :(得分:0)

如果您想在webview中使用html,css开发完整的混合应用程序,则无法在webview中添加任何视图,您可以使用phoneGap。 在你的情况下,如果你想在webview中添加一个按钮,然后在那个按钮上点击想要下载的东西,那么我给你的建议是去PhoneGap。

http://phonegap.com/