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.orgPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 INTHE 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(); }}