版权声明:本文为博主原创文章,未经博主允许不得转载。
第一步:下载jar包添加到项目
需要去腾讯浏览服务官网下载X5内核jar包
第二步:添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
第三步:直接使用
很多文章中说要用到application,其实不用也是可以的。在application中做设置只是增加了预先加载的功能。
MainActivity.java:
public class MainActivity extends Activity {
private X5WebView mWebView;
private String mUrl = "http://www.baidu.com/";
private RelativeLayout rlMore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rlMore = (RelativeLayout) this.findViewById(R.id.rl_more);
rlMore.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v)
{
Intent intent = new Intent(MainActivity.this,UrlSetActivity.class);
MainActivity.this.startActivityForResult(intent,1);
return false;
}
});
rlMore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
loadWebUrl();
}
});
mWebView = (X5WebView) this.findViewById(R.id.awwt_webview);
mWebView.setWebViewClient(client);
}
private void loadWebUrl() {
if(null != mUrl && mUrl.length() > 0)
{
mWebView.loadUrl(mUrl);
}
}
@Override
protected void onResume()
{
super.onResume();
String preferUrl = SharePreferUtils.getSharePreferValue(this,"WEB_URL", "main_page");
if(null != preferUrl && preferUrl.length() > 0)
{
mUrl = preferUrl;
}
mWebView.loadUrl(mUrl);
}
private WebViewClient client = new WebViewClient() {
/**
* 防止加载网页时调起系统浏览器
*/
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
public void onReceivedHttpAuthRequest(WebView webview,
com.tencent.smtt.export.external.interfaces.HttpAuthHandler httpAuthHandlerhost, String host,
String realm) {
boolean flag = httpAuthHandlerhost.useHttpAuthUsernamePassword();
}
@Override
public void onPageFinished(WebView webView, String s)
{
super.onPageFinished(webView, s);
}
@Override
public void onReceivedError(WebView webView, int i, String s, String s1)
{
System.out.println("***********onReceivedError ************" );
super.onReceivedError(webView, i, s, s1);
}
@Override
public void onReceivedHttpError(WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
System.out.println("***********onReceivedHttpError ************" );
super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode == RESULT_OK)
{
if(null != data)
{
String tUrl = data.getStringExtra("WEB_URL");
if(null != tUrl && tUrl.length() > 0)
{
mUrl = tUrl;
mWebView.loadUrl(mUrl);
SharePreferUtils.setSharePreferValue(this,"WEB_URL", "main_page",mUrl);
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy()
{
if (mWebView != null)
{
mWebView.destroy();
}
super.onDestroy();
}
}
代码十分简单,也很容易理解。在这个我加了个跳转类来实现访问其他网页,其实就是想测测X5的性能
public class UrlSetActivity extends Activity {
private ImageView ivBack;
private EditText edContent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_url_set);
edContent = (EditText) this.findViewById(R.id.ampf_et_content);
findViewById(R.id.rl_more).setVisibility(View.GONE);
ivBack = (ImageView) this.findViewById(R.id.iv_back);
ivBack.setVisibility(View.VISIBLE);
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
doBack();
}
});
findViewById(R.id.ampf_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(edContent.getText().toString().length() > 0) {
Intent intent = UrlSetActivity.this.getIntent();
intent.putExtra("WEB_URL",edContent.getText().toString());
UrlSetActivity.this.setResult(RESULT_OK, intent);
UrlSetActivity.this.finish();
}else{
Toast.makeText(UrlSetActivity.this,"网址不能为空",Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onBackPressed()
{
doBack();
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{
doBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void doBack()
{
UrlSetActivity.this.setResult(RESULT_OK, null);
finish();
}
}
网友评论