Cocos2d-JS 調用Java__Java

來源:互聯網
上載者:User

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();    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.