Webview一直在崩溃

时间:2015-10-18 19:50:49

标签: android webview android-webview

以下代码是我用来创建允许显示meleromusic.com的webview的代码。我试图让webview显示网站,但允许外部链接离开应用程序。例如,如果有人点击了一个链接,该链接会将用户带到Play商店安装应用程序,他们只需将webview转到Play商店,而不是在webview上查看游戏商店。

package com.melero.music;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.app.Activity;
import android.webkit.DownloadListener;
import android.app.DownloadManager.Request;
import android.app.DownloadManager;
import android.os.Environment;
import android.net.Uri;
public class MainActivity extends Activity {
private WebView mWebView;

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

    // Allow third party cookies for Android Lollipop
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptThirdPartyCookies(mWebView,true);
    }

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.setWebViewClient(new WebViewClient());
    mWebView.getSettings().setGeolocationEnabled(true);
    mWebView.setWebChromeClient(new WebChromeClient() {
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            // callback.invoke(String origin, boolean allow, boolean remember);
            callback.invoke(origin, true, false);
        }
    });



    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setAppCacheEnabled(true);
    mWebView.getSettings().setDatabaseEnabled(true);
    mWebView.getSettings().setDomStorageEnabled(true);
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.getSettings().setSupportMultipleWindows(true);
    String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();
    mWebView.getSettings().setAllowFileAccess(true);
    mWebView.getSettings().setAppCachePath(appCachePath);
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

    mWebView.setWebViewClient(new WebViewClient(){
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            String url2="http://www.meleromusic.com/";
            // all links  with in ur site will be open inside the webview
            //links that start ur domain example(http://www.example.com/)
            if (url != null && url.startsWith(url2)){
                return false;
            }
            // all links that points outside the site will be open in a normal android browser
            else  {
                view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            }
        }
    });


    mWebView.loadUrl("http://www.meleromusic.com/");
    mWebView.setDownloadListener(new DownloadListener()
    {
        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength)
        {
            //for downloading directly through download manager
            Request request = new Request(Uri.parse(url));
            request.setMimeType("audio/MP3");
            request.allowScanningByMediaScanner();
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, ".mp3");
            DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            dm.enqueue(request);
        }
    });


}














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

    return super.onKeyDown(keyCode, event);
}



}

0 个答案:

没有答案