cocos2dx Android 使用WebView来加载网页

先上代码啦,找到src下你的跟项目名一样的那个java,例如src/com/abc/game/test.java,做如下修改

public class test extends Cocos2dxActivity{
	private static test instance = null;
	WebView m_webView;
	FrameLayout m_webLayout;
    	LinearLayout topLayout;
    	Button backButton;
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		instance = this;
		
		 m_webLayout = new FrameLayout(this);
	     	addContentView(m_webLayout, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
	}
	
	@Override
    	public void onDestroy()
    	{
        	super.onDestroy();

		System.exit(0);
    	}
	
	public static Object getInstance() {  //返回实例给c++调用者
		return eInstance;
	}
	
	public void openURL(String urlString) {
		final String url = urlString;
        this.runOnUiThread(new Runnable() {//在主线程里添加别的控件
            public void run() {   
                //初始化webView
                m_webView = new WebView(instance);
                //设置webView能够执行javascript脚本
                m_webView.getSettings().setJavaScriptEnabled(true);
                //设置可以支持缩放
                m_webView.getSettings().setSupportZoom(true);//设置出现缩放工具
                m_webView.getSettings().setBuiltInZoomControls(true);
                //载入URL
                m_webView.loadUrl(url);
                //使页面获得焦点
                m_webView.requestFocus();
                //如果页面中链接,如果希望点击链接继续在当前browser中响应
                m_webView.setWebViewClient(new WebViewClient(){ 
                	public ProgressDialog loadingView = null;
                    public boolean shouldOverrideUrlLoading(WebView view, String url) {   
                        if(url.indexOf("tel:")<0){
                            view.loadUrl(url); 
                        }
                        return true;       
                    }
                    
                    public void onPageStarted(WebView view, String url, Bitmap favicon) { //读取内容时,增加一个loading界面,防止触碰
                    	if (null == loadingView) {
                    		loadingView = ProgressDialog.show(test.this, "提示", "努力加载中...", true);
                    	}
                    }

                    public void onPageFinished(WebView view, String url) {
                    	if (null != loadingView) {
                    		loadingView.dismiss();
                    		loadingView = null;
                    	}
                    }
                });
                /*背景图*/                
//                m_imageView = new ImageView(instance);
//                m_imageView.setImageResource(R.drawable.lobbybg);
//                m_imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                /*初始化线性布局 里面加按钮和webView*/
                topLayout = new LinearLayout(instance);      
                topLayout.setOrientation(LinearLayout.VERTICAL);
                /*初始化返回按钮*/
                backButton = new Button(instance);
//                backButton.setBackgroundResource(R.drawable.backbutton);
                backButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                backButton.setText("关闭");
                backButton.setTextColor(Color.argb(255, 0, 0, 0));
                backButton.setTextSize(14);                
                backButton.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						// TODO Auto-generated method stub
						removeWebView();
					}
                });
                //把image加到主布局里
//                m_webLayout.addView(m_imageView);
                //把webView加入到线性布局 
                topLayout.addView(backButton);
                topLayout.addView(m_webView);
                //再把线性布局加入到主布局
                m_webLayout.addView(topLayout);
            }
        });
    }
	
	
    //移除webView  把刚才加的所有控件都删掉
    public void removeWebView() {              
//        m_webLayout.removeView(m_imageView);
//        m_imageView.destroyDrawingCache();
        
        m_webLayout.removeView(topLayout);
        topLayout.destroyDrawingCache();
                
        topLayout.removeView(m_webView);
        m_webView.destroy();
                
        topLayout.removeView(backButton);
        backButton.destroyDrawingCache();
    }
	
	
	public Cocos2dxGLSurfaceView onCreateGLSurfaceView() {
    	return new LuaGLSurfaceView(this);
    }

    static {
        System.loadLibrary("cocos2dlua");
    }
}

这里是简单地弹出一个网页,上方保留一行空位,左上方放一个返回按钮

在c++调用者那里,使用以下方法就能调用

void Util::openURL(const char* pszUrl)
{
    
    JniMethodInfo minfo;
    jobject jobj;
    bool isHave = JniHelper::getStaticMethodInfo(minfo, "com/evil/game/evilcard", "getInstance", "()Ljava/lang/Object;");
    
    if (!isHave) {
        return;
    }
    jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID);
    isHave = JniHelper::getMethodInfo(minfo, "com/evil/game/evilcard", "openURL", "(Ljava/lang/String;)V");
    if (!isHave) {
        return;
    }
    jstring StringArg1 = minfo.env->NewStringUTF(pszUrl);
    minfo.env->CallVoidMethod(jobj, minfo.methodID, StringArg1);
    minfo.env->DeleteLocalRef(StringArg1);
    minfo.env->DeleteLocalRef(minfo.classID);
}


然后就可以通过 Util::openURL("http://www.baidu.com");来调用啦~

你可能感兴趣的:(cocos2d-x,Android)