安卓调用手机自带的浏览器

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pyf_914406232/article/details/78185414

安卓相关

1. 在游戏里面如何通过调用手机浏览器打开一个网页

cocos2d自带一个方法:

                      cc.Application:getInstance():openURL("http://www.559007.com");

这个方法是官方自带的。

我们首先来看看在两大移动OS上的实现,其他平台也都有,不过一般我不用,也就不关心了,我们在cocos2d-x/cocos/platform/ios/CCApplication-ios.mm文件中可以看到IOS的实现,如下

bool Application::openURL(const std::string &url)  

{  

    NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];  

    NSURL* nsUrl = [NSURL URLWithString:msg];  

    return [[UIApplication sharedApplication] openURL:nsUrl];  

}  

同样,我们可以在cocos2d-x/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java文件中看到android中的实现

public static boolean openURL(String url) {   

    boolean ret = false;  

    try {  

        Intent i = new Intent(Intent.ACTION_VIEW);  

        i.setData(Uri.parse(url));  

        sActivity.startActivity(i);  

        ret = true;  

    } catch (Exception e) {  

    }  

    return ret;  

}  

既然有了OS层实现,我们游戏逻辑层怎么使用,比如我使用的是lua开发,那我就去找lua绑定的文件,看它绑定到哪里了,我们在cocos2d-x/cocos/scripting/lua-bindings/auto/lua_cocos2dx_auto.cpp文件中可以看到如下方法

int lua_register_cocos2dx_Application(lua_State* tolua_S)  

{  

    tolua_usertype(tolua_S,"cc.Application");  

    tolua_cclass(tolua_S,"Application","cc.Application","",nullptr);  

  

    tolua_beginmodule(tolua_S,"Application");  

        tolua_function(tolua_S,"openURL",lua_cocos2dx_Application_openURL);  

        tolua_function(tolua_S,"getTargetPlatform",lua_cocos2dx_Application_getTargetPlatform);  

        tolua_function(tolua_S,"getCurrentLanguage",lua_cocos2dx_Application_getCurrentLanguage);  

        tolua_function(tolua_S,"getCurrentLanguageCode",lua_cocos2dx_Application_getCurrentLanguageCode);  

        tolua_function(tolua_S,"setAnimationInterval",lua_cocos2dx_Application_setAnimationInterval);  

        tolua_function(tolua_S,"getInstance", lua_cocos2dx_Application_getInstance);  

    tolua_endmodule(tolua_S);  

    std::string typeName = typeid(cocos2d::Application).name();  

    g_luaType[typeName] = "cc.Application";  

    g_typeCast["Application"] = "cc.Application";  

    return 1;  

}  

从中我们可以看到这两句

tolua_beginmodule(tolua_S,"Application");  

        tolua_function(tolua_S,"openURL",lua_cocos2dx_Application_openURL);

这就表明这个openURL的方法确实被绑定到Application对象上了,具体绑定的实现是这个方法,有兴趣的可以去深入看看

那我们在lua层就可以直接通过获得Application来调用了,使用如下:

cc.Application:getInstance():openURL("http://play.cn")

还有一种比较低级的手动方法:

function onStartOpenUrl(url)

    print("onStartOpenUrl  -----")

    if device.platform == "ios" then

        -- local args = {serverID="3"}

        -- local ok,ret  = luaCallFun.callStaticMethod("MethodForLua","PlatformSwtichServer",args)

        -- if ok == false then

        --     prints("luaoc调用出错:PlatformSwtichServer")

        -- end

        

    elseif device.platform == "android" then

        print("start lua to java");

        local javaMethodName = "openUrl"

        local javaParams = {url}

        local javaMethodSig = "(Ljava/lang/String;)V"

        local ok,ret  = luaCallFun.callStaticMethod(javaClassName, javaMethodName,javaParams,javaMethodSig)

        if ok == false then

            prints("luaoc调用出错 PlatformSwtichServer")

        end

    else

        prints("onUnitedPlatformLogin!")

    end

End

在java中的代码如下:

public static void openUrl(String url){

  Intent intent = new Intent();

  //intent.putExtra("landscape", "portrait");

         intent.setAction("android.intent.action.VIEW");           

         Uri content_url = Uri.parse(url);

         intent.setData(content_url);

         instance.startActivity(intent);

  }

小知识:

安卓更改包名:把清单文件AndroidManifest.xml中的包名改成你想要的,然后再把你工程中用到包名的地方修改一下就可以了。

修改工程名:点击选中你想要更改的工程,然后快捷键F2就可以实现更改工程名字。

猜你喜欢

转载自blog.csdn.net/pyf_914406232/article/details/78185414