cocos2d / cocos2d-js

cocos2d-x for JS
http://www.cocos2d-x.org
MIT License
1.86k stars 487 forks source link

Add native class refrection #1741

Open jianglong0156 opened 9 years ago

jianglong0156 commented 9 years ago

add a importClass mehtod to get the native class like this to get the device.uuid:

var Settings= plus.android.importClass("android.provider.Settings");

console.log(Settings.Secure.getString(mainActivity.getContentResolver(),Settings.Secure.ANDROID_ID));

create the icon in deskop:

Intent = plus.android.importClass("android.content.Intent"); // create icon var shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); // set icon name shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "test icon"); // set no repeat shortcut.putExtra("duplicate",false); // set icon var iconPath = plus.io.convertLocalFileSystemURL("/icon.png"); var bitmap = BitmapFactory.decodeFile(iconPath); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,bitmap);

var action = new Intent(Intent.ACTION_MAIN);
action.setComponent(main.getComponentName());
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,action);

main.sendBroadcast(shortcut);
jianglong0156 commented 9 years ago

the docuemt http://ask.dcloud.net.cn/article/88

zj8487 commented 9 years ago

+1

zhaijialong commented 9 years ago

Looks cool, but really need too much work to achieve this feature. It's very similar to NativeScript, and you can find how they implement this feature in https://github.com/NativeScript/android-runtime.

I think a simpler and easier way is to implement the addJavascriptInterface just like Android SDK's webview which can do pretty much the same thing. http://developer.android.com/reference/android/webkit/WebView.html#addJavascriptInterface(java.lang.Object, java.lang.String)

BTW, egret also has something very similar to the addJavascriptInterface.