您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-JS 调用Java

2016-09-19 13:46 417 查看
JS:

//调用hello方法
jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Test", "hello", "(Ljava/lang/String;)V", "this is a message from js");

//调用第一个sum方法
var result = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Test", "sum", "(II)I", 3, 7);
cc.log(result); //10

//调用第二个sum方法
var result = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Test", "sum", "(I)I", 3);
cc.log(result); //5

var udid = jsb.reflection.callStaticMethod("org/cocos2dx/javascript/Test", "UDID", "()Ljava/lang/String;");
cc.log("UDID ==>" + udid);

Java:Test.Java

/****************************************************************************
Copyright (c) 2015 Chukong Technologies Inc.
 http://www.cocos2d-x.org 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.javascript;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

import com.google.android.gms.common.api.GoogleApiClient;

import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

public class AppActivity extends Cocos2dxActivity {

/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client2;

@Override
public Cocos2dxGLSurfaceView onCreateView() {
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
// TestCpp should create stencil buffer
glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);

return glSurfaceView;
}

/**
* deviceID的组成为:渠道标志+识别符来源标志+hash后的终端识别符
* <p/>
* 渠道标志为:
* 1,andriod(a)
* <p/>
* 识别符来源标志:
* 1, wifi mac地址(wifi);
* 2, IMEI(imei);
* 3, 序列号(sn);
* 4, id:随机码。若前面的都取不到时,则随机生成一个随机码,需要缓存。
*
* @param context
* @return
*/
public static String UDID() {

StringBuilder deviceId = new StringBuilder();
// 渠道标志
deviceId.append("a");

try {
// wifi mac地址
WifiManager wifi = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifi.getConnectionInfo();
String wifiMac = info.getMacAddress();
if (!wifiMac.isEmpty()) {
deviceId.append("wifi");
deviceId.append(wifiMac);
return deviceId.toString();
}

// IMEI(imei)
TelephonyManager tm = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
if (imei.isEmpty()) {
deviceId.append("imei");
deviceId.append(imei);
return deviceId.toString();
}

// 序列号(sn)
String sn = tm.getSimSerialNumber();
if (!sn.isEmpty()) {
deviceId.append("sn");
deviceId.append(sn);
return deviceId.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return deviceId.toString();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: