Unity3D游戲引擎實(shí)現(xiàn)在Android中打開WebView的實(shí)例
本文講述了如何在Unity中調(diào)用Android中的WebView組件,實(shí)現(xiàn)內(nèi)部瀏覽器樣式的頁面切換。首先打開Eclipse創(chuàng)建一個(gè)Android的工程:
UnityTestActivity.java 入口Activity ,Unity中會(huì)調(diào)用這個(gè)Activity中的方法從而打開網(wǎng)頁。
package com.xys;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.unity3d.player.UnityPlayerActivity;
public class UnityTestActivity extends UnityPlayerActivity {
Context mContext = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
}
//Unity中會(huì)調(diào)用這個(gè)方法,從而開打WebView
public void StartWebView(String str)
{
Intent intent = new Intent(mContext,WebViewActivity.class);
this.startActivity(intent);
}
}
WebViewActivity.java Unity中發(fā)出通知打開這個(gè)Activity 繼而打開WebView,沒有什么難點(diǎn)大家看看就應(yīng)當(dāng)能掌握。
package com.xys;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("http://www.baidu.com/");
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
close = (Button) findViewById(R.id.button);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
WebViewActivity.this.finish();
}
});
}
private class WebViewClient extends android.webkit.WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//這里實(shí)現(xiàn)的目標(biāo)是在網(wǎng)頁中繼續(xù)點(diǎn)開一個(gè)新鏈接,還是停留在當(dāng)前程序中
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
}
}
然后是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="關(guān)閉網(wǎng)頁"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
最后是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xys"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".UnityTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".WebViewActivity">
</activity>
</application>
<!-- 連接互聯(lián)網(wǎng)的權(quán)限 -->
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
OK 到這里JAVA代碼已經(jīng)完全寫完,然后把所有.JAVA文件打包變成.class文件,具體轉(zhuǎn)換的方法大家可以參照相關(guān)的文章,這里就不再重復(fù)介紹了。
- Android仿開心消消樂大樹星星無限循環(huán)效果
- Android游戲源碼分享之2048
- Android游戲開發(fā)實(shí)踐之人物移動(dòng)地圖的平滑滾動(dòng)處理
- Android 游戲開發(fā)之Canvas畫布的介紹及方法
- Android游戲開發(fā)之碰撞檢測(矩形碰撞、圓形碰撞、像素碰撞)
- Android五子棋游戲程序完整實(shí)例分析
- 以一個(gè)著色游戲展開講解Android中區(qū)域圖像填色的方法
- Android高仿2048小游戲?qū)崿F(xiàn)代碼
- Android開心消消樂代碼實(shí)例詳解
- Android 2d游戲開發(fā)之貪吃蛇基于surfaceview
相關(guān)文章
Android使用lottie加載json動(dòng)畫的示例代碼
本篇文章主要介紹了Android使用lottie加載json動(dòng)畫的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
Android 如何實(shí)現(xiàn)彈窗順序&優(yōu)先級(jí)控制
這篇文章主要介紹了Android 如何實(shí)現(xiàn)彈窗順序&優(yōu)先級(jí)控制,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法,涉及Android處理壓縮文件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android利用CursorLoader實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫
這篇文章主要為大家詳細(xì)介紹了Android利用CursorLoader實(shí)現(xiàn)短信驗(yàn)證碼自動(dòng)填寫的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
android編程之menu按鍵功能實(shí)現(xiàn)方法
這篇文章主要介紹了android編程之menu按鍵功能實(shí)現(xiàn)方法,實(shí)例分析了Android實(shí)現(xiàn)menu的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04

