安卓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>
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>
相关文章推荐
- android webview type=file文件上传,安卓端代码
- 用WebView加载Html代码片段
- 安卓WebView中接口隐患(远程代码执行漏洞)与手机挂马利用
- 安卓使用webView中的JavaScript代码
- 调用代码JCFXBL与WebView整合打造安卓应用服务平台
- WebView加载部分Html代码
- Android中WebView载入本地HTML代码并实现Java与JavaScript交互的例子
- Android WebView学习,创建步骤并附代码(Handler、线程、WebView结合)
- 测试2----Javascript代码片段之Event封装
- Android中WebView载入本地HTML代码并实现Java与JavaScript交互
- js页面调用WebView中java代码初步
- android:如何在java代码中覆盖webview中的javascript事件
- 在sdk的17以上版本webview中js不能调用java代码
- 安卓:WebView中iframe,焦点字段出现两个文本输入框,位置错误
- Android WebView中的JavaScript代码使用
- MR_Android View Client 问题总结及代码片段
- android webview显示HTML代码
- WebView 支持flash加载代码
- 用几十行代码写一个可以在PC Web,PC桌面,安卓,iOS上运行的程序
- Android WebView中的JavaScript代码使用