Andriod 中打开WebView
来源:互联网 发布:大智慧大数据终端 编辑:程序博客网 时间:2024/06/10 05:40
今天了解了一下Andiod的WebView方面的知识,感觉非常的神奇。我发现我知道的东西还是太少了,还是要Good Good Study,Day Day Up。
这篇博客是参考雨松MOMO的博客Unity3D研究院之在Android中打开WebView(三十) ,向大神致敬!此处省略几百万字。。。
第一步:首先在unity导出的andriod工程的UnityPlayerNativeActivity.java文件中,加入一个StartWebView函数,方便游戏中直接调用。
package com.dong.mymta;import com.unity3d.player.*;import android.app.NativeActivity;import android.content.Intent;import android.content.res.Configuration;import android.graphics.PixelFormat;import android.os.Bundle;import android.view.KeyEvent;import android.view.View;import android.view.Window;import android.view.WindowManager;public class UnityPlayerNativeActivity extends NativeActivity{protected UnityPlayer mUnityPlayer;// don't change the name of this variable; referenced from native code// UnityPlayer.init() should be called before attaching the view to a layout - it will load the native code.// UnityPlayer.quit() should be the last thing called - it will unload the native code.protected void onCreate (Bundle savedInstanceState){requestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);getWindow().takeSurface(null);setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);getWindow().setFormat(PixelFormat.RGB_565);mUnityPlayer = new UnityPlayer(this);if (mUnityPlayer.getSettings ().getBoolean ("hide_status_bar", true))getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1);boolean trueColor8888 = false;mUnityPlayer.init(glesMode, trueColor8888);View playerView = mUnityPlayer.getView();setContentView(playerView);playerView.requestFocus();}//Unity中会调用这个方法,从而开打WebView public void StartWebView(String url) { Intent intent = new Intent(UnityPlayerNativeActivity.this,WebViewActivity.class); intent.putExtra("url", url); this.startActivity(intent); }}
Intent 这个类不太清楚的可以看下这篇博客,Android Intent Action ,这里面有将这个类的一些用途,值得一看。。。
第二步,只要是WebViewActivity.java这个类
package com.dong.mymta;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.webkit.WebView;import android.widget.Button;public class WebViewActivity extends Activity{private WebView webView;private Button close;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState); setContentView(R.layout.main); String url = this.getIntent().getStringExtra("url");webView = (WebView) findViewById(R.id.webView);webView.loadUrl(url); webView.getSettings().setJavaScriptEnabled(true);webView.setWebViewClient(new WebViewClient()); close = (Button) findViewById(R.id.button);close.setOnClickListener(new OnClickListener() { @Overridepublic void onClick(View v) {WebViewActivity.this.finish(); }});}private class WebViewClient extends android.webkit.WebViewClient {@Overridepublic boolean shouldOverrideUrlLoading(WebView view, String url) {//这里实现的目标是在网页中继续点开一个新链接,还是停留在当前程序中view.loadUrl(url);return super.shouldOverrideUrlLoading(view, url); }}}
WebViewActivity.java里面的界面需要在layout配置文件main.xml中给出
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <WebView android:id="@+id/webView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" /> <Button android:id="@+id/button" android:text="关闭网页" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
具体界面如图所示
图中的WebViewj就是显示范围,可以自己调整。
第三部,修改配置文件AndriodManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dong.mymta" android:theme="@android:style/Theme.NoTitleBar" android:versionName="1.0" android:versionCode="1" android:installLocation="preferExternal"> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" /> <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false"> <activity android:label="@string/app_name" android:screenOrientation="portrait" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale" android:name="com.dong.mymta.UnityPlayerNativeActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> <meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="false" /> </activity> <span style="color:#ff0000;"><activity android:name=".WebViewActivity"> </activity></span> </application> <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="17" /> <uses-feature android:glEsVersion="0x00020000" /> <!-- 连接互联网的权限 --> <span style="color:#ff0000;"><uses-permission android:name="android.permission.INTERNET" /></span></manifest>
第四步,andriod环境下的东西已经搞定了,接下来就在unity中调用StartWebView这个函数了。
using UnityEngine;using UnityEngine;using System.Collections;public class Test2 : MonoBehaviour { public string url = "http://www.baidu.com"; void OnGUI() { if (GUI.Button(new Rect(300, 200, 100, 60), "Open World")) { if (Application.platform == RuntimePlatform.Android) { AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject jo = jc.GetStatic<androidjavaobject>("currentActivity"); jo.Call("StartWebView", url); } } }}</androidjavaobject>
我们把要打开的链接传过去就能打开对应的网页了。接下来又到了上图的时候了。
。。。
0 0
- Andriod 中打开WebView
- 在Webview中打开app
- andriod webview加载地址
- andriod webview 控件
- 【项目中---WebView】Webview打开本地图片文件选择解决方案
- WebView中使用HTML打开本地应用
- webview 在自己应用中打开网页
- Android开发 WebView新窗口中打开链接
- WebView中运用HTML打开本地应用
- WebView中使用HTML打开本地应用
- WebView中使用HTML打开本地应用
- WebView中点击链接使用自身打开
- webview如何在自身Activity中打开
- Android中 WebView打开一个网页实例
- Andriod webview实现文件上传
- Andriod webview实现文件上传
- Andriod webview实现文件上传
- Andriod webview实现文件上传
- Lua相关网站
- PHP获取今天、昨天、明天的日期
- 2015年南通二级建造师考试报名通知
- Groovy入门教程
- MyEclipse消除Cannot return from outside a function or method.
- Andriod 中打开WebView
- SQL聚合函数
- 设置指定目录的SVN分支
- PAT 04-2. File Transfer (25) (并查集)
- java面向对象和类
- iReport 按某个字段(属性)值分页打印
- reason: 'Could not load NIB in bundle: "XXXXXXX" with name 'xxCell'
- my git log
- POJ-3984-迷宫问题