2 yangtao 1 yangtao_1 于 2014.05.22 13:09 提问

Unity点击按钮访问Android的WebView页面。

** Android代码(第一个类): **
package com.xys;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
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中会调用这个方法,从而开打WebView
     public void StartWebView(String str)
     {       
             Toast.makeText(UnityTestActivity.this, "msksjajkajjfk", 2000).show();
             Intent intent = new Intent(mContext,WebViewActivity.class);
             this.startActivity(intent);

     }

}
** (第二个类) **
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;
import android.widget.Toast;

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.activity_main);

            webView = (WebView) findViewById(R.id.webView);
            webView.loadUrl("http://www.baidu.com/");
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setUseWideViewPort(true) ;
            webView.getSettings().setLoadWithOverviewMode(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) {
                    //这里实现的目标是在网页中继续点开一个新链接,还是停留在当前程序中
                    view.loadUrl(url);
                   return true;

            }
    }

}

** xml: **
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
/>
android:id="@+id/button"
android:text="关闭网页"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

** AndroidManifest.xml: **
<?xml version="1.0" encoding="utf-8"?>
package="com.xys"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<uses-permission  android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".WebViewActivity"
        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=".UnityTestActivity"></activity>
</application>

** Unity代码: **
using UnityEngine;
using System.Collections;

public class Test : MonoBehaviour {

        // Update is called once per frame

        void Update ()

{

            if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home) )
            {

                    Application.Quit();
            }
}

void OnGUI()

{
if(GUILayout.Button("OPEN WEBVIEW",GUILayout.Height(100)))

    {
        AndroidJavaClass jc = new AndroidJavaClass("com.xys");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("UnityTestActivity");
        jc.Call("StartWebView","");
    }
    }

}
(注:android代码中.class文件已经打成jar格式,也放入了Unity中)。Unity中Bundle Identifier中写的是(com.xys)。在Android程序那边写了一个Toast(),但在真机上运行的时候,却不会弹。应该是Unity那边调Android方法错了,但不知道怎么改,求各位帮帮忙拉。非常感谢。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!