- 首先需要在activity_main.xml文件里面添加WebView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<WebView
android:id="@+id/game_context"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
- AndroidManifest.xml文件里面添加相关权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.h5gameplay"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar" >
<activity
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>
- 最后在MainActivity.java里面实现WebView的相关代码
public class MainActivity extends Activity {
private String TAG = "MainActivity";
private WebView gameWebView;
private String rootUrl = "涉及公司隐私,所以H5游戏链接不能公布";
static Activity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
instance = this;
init();
}
@SuppressLint("JavascriptInterface")
private void init()
{
//com.tencent.smtt.sdk.WebView
gameWebView = (WebView) findViewById(R.id.game_context);
gameWebView.loadUrl(rootUrl);
WebSettings webSettings = gameWebView.getSettings();
webSettings.setJavaScriptEnabled(true); //支持js
webSettings.setDomStorageEnabled(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); //提高渲染的优先级
// 设置自适应屏幕,两者合用
webSettings.setUseWideViewPort(true); //将图片调整到适合webview的大小
webSettings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
webSettings.setSupportZoom(true); //支持缩放,默认为true。是下面那个的前提。
webSettings.setBuiltInZoomControls(true); //设置内置的缩放控件。
//若上面是false,则该WebView不可缩放,这个不管设置什么都不能缩放。
webSettings.setDisplayZoomControls(false); //隐藏原生的缩放控件
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局
webSettings.supportMultipleWindows(); //多窗口
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); //关闭webview中缓存
webSettings.setAllowFileAccess(true); //设置可以访问文件
webSettings.setNeedInitialFocus(true); //当webview调用requestFocus时为webview设置节点
webSettings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
webSettings.setLoadsImagesAutomatically(true); //支持自动加载图片
gameWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e(TAG, "shouldOverrideUrlLoading: " + url);
boolean isGameUrl = url.indexOf("http://h5.wuzhiyou.com/game/api")!=-1;
boolean isWxpay = url.indexOf("type=wxpay")!=-1;
boolean isAlipay = url.indexOf("type=alipay")!=-1;
if (isGameUrl&&isAlipay) {
Log.i(TAG,"the url ->"+url);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .setData(Uri.parse(url));
startActivity(intent);
return true;
}
if (isGameUrl&&isWxpay) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent .setData(Uri.parse(url));
startActivity(intent);
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
@Override
public void onPageFinished(WebView view, String url) {
Log.e(TAG, "onPageFinished: " + url);
// 这个方法有可能会多次执行
super.onPageFinished(view, url);
if(url.startsWith("http://h5.wuzhiyou.com/game/gameStart?"))
{
Context context = MainActivity.this;
String net=DeviceInfoTool.getNet(context);
int version=DeviceInfoTool.getOS_version();
String idString=DeviceInfoTool.getDeviceId(context);
String typeString=DeviceInfoTool.getDeviceType();
view.loadUrl("javascript:setNet('"+net+"','"+version+"','"+idString+"','"+typeString+"')");
}
}
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
gameWebView.setWebChromeClient(new WebChromeClient(){
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
// TODO Auto-generated method stub
return super.onJsAlert(view, url, message, result);
}
@Override
public boolean onJsConfirm(WebView view, String url,
String message, JsResult result) {
// TODO Auto-generated method stub
return super.onJsConfirm(view, url, message, result);
}
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, JsPromptResult result) {
// TODO Auto-generated method stub
return super.onJsPrompt(view, url, message, defaultValue, result);
}
});
gameWebView.loadUrl(rootUrl);
gameWebView.requestFocus();
}
@Override
protected void onDestroy() {
gameWebView.removeAllViews();
gameWebView.destroy();
super.onDestroy();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && gameWebView.canGoBack()) {
gameWebView.goBack();// 返回前一个页面
return true;
}
return super.onKeyDown(keyCode, event);
}
}
网友评论