安卓webview封装代码片段,留个备份
来源:互联网 发布:网络语言汉字拆分 编辑:程序博客网 时间:2024/04/25 21:52
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封装代码片段,留个备份
- 安卓常用代码片段
- 代码片段的备份
- 安卓加载html代码片段
- 安卓使用webView中的JavaScript代码
- 有用的,用过的安卓代码片段
- 安卓代码片段,持续更新用于积累
- 安卓必备五个常用代码片段整理
- 用WebView加载Html代码片段
- 50个jquery代码片段
- 50个jquery代码片段
- 15个jQuery代码片段
- 10个PHP代码片段
- 15个jQuery代码片段
- 15个jQueryMobile代码片段
- 10个jQuery代码片段
- 48个javascript代码片段
- 48 个 JavaScript 代码片段
- STL容器的适用情况和缺点
- poj1847 dijkstra板子
- PHP - 文件系统
- 子类在new一个实例时,与父类构造方法的联系
- iTween基础之Rotate
- 安卓webview封装代码片段,留个备份
- 自定义View之WiperSwitch改进版
- TLSv2原理和Wireshark验证(详细)
- C++枚举类型
- iTween基础之Audio(音量和音调的变化)
- 类型
- Mac React-Native环境搭建及使用
- iTween基础之Punch(摇晃)
- Android仿网络直播弹幕功能的实现