您的位置:首页 > 移动开发

安卓webview封装代码片段,留个备份

2016-11-06 14:42 288 查看
MainActivity.java

package com.example.ear;

import android.os.Bundle;

import android.content.res.Configuration;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.util.Log;

import android.webkit.DownloadListener;

import android.view.KeyEvent;

import android.webkit.WebSettings;

import android.webkit.WebView;

import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    private WebView webview;

    @SuppressLint("SetJavaScriptEnabled")

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        webview = (WebView) findViewById(R.id.webview);

        webview.setBackgroundColor(0xFF31C27C);

        WebSettings webSettings = webview.getSettings();

        webSettings.setJavaScriptEnabled(true);

        webSettings.setAllowFileAccess(true);

        webview.loadUrl("http://www.erduo.in");

        webview.setWebViewClient(new webViewClient());

        webview.setDownloadListener(new MyWebViewDownLoadListener());

    }

    @Override

    public void onConfigurationChanged(Configuration config) {

        super.onConfigurationChanged(config);

    }

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {

            webview.goBack();

            return true;

        }

        return super.onKeyDown(keyCode, event);

    }

    private class webViewClient extends WebViewClient {

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);

            return true;

        }

    }

    private class MyWebViewDownLoadListener implements DownloadListener {

        @Override

        public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {

            Log.i("tag", "url="+url);

            Log.i("tag", "userAgent="+userAgent);

            Log.i("tag", "contentDisposition="+contentDisposition);

            Log.i("tag", "mimetype="+mimetype);

            Log.i("tag", "contentLength="+contentLength);

            Uri uri = Uri.parse(url);

            Intent intent = new Intent(Intent.ACTION_VIEW, uri);

            startActivity(intent);

        }

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <WebView

        android:id="@+id/webview"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:layerType="software"

        />

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.ear"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="11"

        android:targetSdkVersion="19" />

    <uses-permission

        android:name="android.permission.INTERNET" >

    </uses-permission>

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:theme="@android:style/Theme.Holo.NoActionBar"

            android:configChanges="orientation|screenSize"

            android:name=".MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  webview html5 java android