[工作积累] Android system dialog with native callback
JNI: invoke java dialog with native callback:
store native function address in java, and invoke native another method to this function.
key points:
1.Store callback function pointer(address) in java: to support multiple calls, use data block for each call.
2.Use primitive long(64bit) in Java to store native callback pointer, for 64 bit native compatibility
3.intptr_t for pointer in native.
4.Show dialog in UI thread (Activity.runOnUiThread )
5.optional: Let the native code to handle localization (minimize Java code)
Java:
public static native void nativeOnSystemDialogResult(long nativeFuncAddr);
class DialogRunnable implements Runnable {
public String mTitle;
public String mMessage;
public String mYes;
public String mNo;
public long mOnYesAddr;
public long mOnNoAddr;
public boolean mTwoButton;
//////////////////////////////////////////////////////////////////////////
///title, message, localized Yes No
DialogRunnable(String tittle, String message, String locYes, String locNo, long onYesAddr, long onNoAddr, boolean twoButton)
{
mTitle = tittle;
mMessage = message;
mYes = locYes;
mNo = locNo;
mOnYesAddr = onYesAddr;
mOnNoAddr = onNoAddr;
mTwoButton = twoButton;
}
//////////////////////////////////////////////////////////////////////////
public void run() {
if( mTwoButton ) {
Dialog dialog = new AlertDialog.Builder( GameActivity.getInstance() )
.setTitle(mTitle)
.setMessage(mMessage)
.setPositiveButton( mYes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
GameActivity.getInstance().nativeOnSystemDialogResult( mOnYesAddr );
}
})
.setNegativeButton( mNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
GameActivity.getInstance().nativeOnSystemDialogResult( mOnNoAddr );
}
})
.setCancelable(false)
.create();
dialog.show();
}else {
Dialog dialog = new AlertDialog.Builder( GameActivity.getInstance() )
.setTitle(mTitle)
.setMessage(mMessage)
.setPositiveButton( mNo, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
GameActivity.getInstance().nativeOnSystemDialogResult( mOnYesAddr );
}
})
.setCancelable(false)
.create();
dialog.show();
}
}
}
//////////////////////////////////////////////////////////////////////////
//Cooperate with native code. DO NOT call on Java
//////////////////////////////////////////////////////////////////////////
public void showDialogYesNo(String title, String showText, String locYes, String locNo, long onYesAddr, long onNoAddr) {
this.runOnUiThread( new DialogRunnable(title, showText, locYes, locNo, onYesAddr, onNoAddr, true) );
}
C (for C++, JNI calls are simpler & different)
JNIEXPORT void JNICALL Java_com_org_package_GameActivity_nativeOnSystemDialogResult(JNIEnv *env, jobject thiz, jlong functionAddr)
{
typedef void(*FUNCPTR)(void);
FUNCPTR ptr = (FUNCPTR)(void*)function;
if( ptr != null )
ptr();
}
//////////////////////////////////////////////////////////////////////////
//Note: onOK & onCancel can be NULL
void Android_SystemDialog(const char* title, const char* message, const char* yes, const char* no, void(*onOK)(void), void(*onCancel)(void) )
{
android_app* app = GetApp();
JNIEnv* env = app->activity->env;
//note: we need to attach dalvik VM to current thread, as it is not main thread
JavaVM* vm = app->activity->vm;
if ( (*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_6) < )
(*vm)->AttachCurrentThread(vm, &env, NULL); jclass ActivityClass = (*env)->GetObjectClass(env, app->activity->clazz);
jmethodID java_method = (*env)->GetMethodID(env, ActivityClass,
(char8*)"showDialogYesNo",
(char8*)"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JJ)V");
assert( java_method != NULL ); jstring jTitle = (*env)->NewStringUTF(env, title, strlen(title) );
jstring jMsg = (*env)->NewStringUTF(env, message, strlen(message) );
jstring jOK = (*env)->NewStringUTF(env, yes, strlen(yes) );
jstring jCancel = (*env)->NewStringUTF(env, no, strlen(no) );
//Note: jlong is 64 bit
jlong jOnOK = (intptr_t)onOK;
jlong jOnCancel = (intptr_t)onCancel;
//invoke UI Dialog in another thread
(*env)->CallVoidMethod(env, app->activity->clazz , java_method, jTitle, jMsg, jOK, jCancel, jOnOK, jOnCancel); (*env)->DeleteLocalRef(env, jTitle);
(*env)->DeleteLocalRef(env, jMsg);
(*env)->DeleteLocalRef(env, jOK);
(*env)->DeleteLocalRef(env, jCancel);
(*env)->DeleteLocalRef(env, ActivityClass);
}
native usage sample:
static void OnExit()
{
exit();
} void Android_Confirm_Exit()
{
const char* title = "Quit";
const char* message = "Unsaved progress will be lost.\nAre you sure you want to quit game?";
const char* yes = "Ok";
const char* no = "Cancel";
Android_SystemDialog(title, message, yes, no, &OnExit, NULL);
}
[工作积累] Android system dialog with native callback的更多相关文章
- [工作积累] android 中添加libssl和libcurl
1. libssl https://github.com/guardianproject/openssl-android 然后执行ndk-build 2.libcurl 源代码组织结构, 下面的mak ...
- [工作积累] Android dynamic library & JNI_OnLoad
Bionic libc doesn't load dependencies for current .so file (diff from Windows or Linux) so a explici ...
- [工作积累] Android: Hide Navigation bar 隐藏导航条
https://developer.android.com/training/system-ui/navigation.html View decorView = getWindow().getDec ...
- Android PopupWindow Dialog 关于 is your activity running 崩溃详解
Android PopupWindow Dialog 关于 is your activity running 崩溃详解 [TOC] 起因 对于 PopupWindow Dialog 需要 Activi ...
- Android 封装Dialog
package com.example.myandroid01; import android.support.v7.app.ActionBarActivity; import android.os. ...
- [工作记录] Android OpenGL ES: non-square texture - continue
previous: [工作记录] Android OpenGL ES 2.0: square texture not supported on some device recently I found ...
- at android.view.Surface.unlockCanvasAndPost(Native Method)
at android.view.Surface.unlockCanvasAndPost(Native Method) 在绘制动画特效的时候点击back键会报以上异常. 主要原因:当点击back按钮时A ...
- Android Theme.Dialog 到光 AppCompatDialog
我用在我的 style.xml 作为主要应用程序主题 <style name="AppTheme" parent="Theme.AppCompat.Light&qu ...
- Error处理: android.media.MediaRecorder.start(Native Method) 报错:start failed: -19【转】
本文转载自:http://blog.csdn.net/netwalk/article/details/17686993 Error处理: android.media.MediaRecorder.sta ...
随机推荐
- 重拾C,一天一点点_12
连续两天没写了,今天继续! sizeof 对象 或 sizeof (类型名) 返回一个整型值,等于指定对象或类型占用的存储空间字节数.(返回值是无符号整型值,其类型为size_t,在头文件<st ...
- LtUpload上传组件
<?php/** * The Upload class * @author Alex Lee <iuyes@qq.com> * @license http://opensource. ...
- python中读取配置文件ConfigParser
在程序中使用配置文件来灵活的配置一些参数是一件很常见的事情,配置文件的解析并不复杂,在python里更是如此,在官方发布的库中就包含有做这件事情的库,那就是ConfigParser,这里简单的做一些介 ...
- rspec的一些常见用法
这里讲了如何安装rspec,安装使用rspec. 下面介绍一下rspec中常见的使用方法. 下面是一个最简单的测试用例,判断true是不是等于true,should_be是旧的用法,新用法推荐使用ex ...
- Linux基础 30分钟GDB调试快速突破
引言 Linus心灵鸡汤 在*nix开发中有道卡叫gdb调试,不管你怎么搞. 它依然在那丝毫不会松动.今天致敬一个 活着的传奇 Linus Torvalds Unix 始于上个世纪60年代,在70年代 ...
- linux kernel 0.11 setup
setup作用 ①读取参数放在0x90000处. ②将原本在0x10000处的system模块移至0x00000处 ③加载中断描述符表,全局描述符表,进入32位保护模式. 概念 关于实模式和保护模式区 ...
- poj 2777 Count Color
题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...
- Go中简单的文件读写
Go中的ioutil包可以方便的实现文件读写.代码: package main import ( "fmt" "io/ioutil" ) func main() ...
- MongoDB的交互(mongodb/node-mongodb-native)、MongoDB入门
MongoDB 开源,高性能的NoSQL数据库:支持索引.集群.复制和故障转移.各种语言的驱动程序:高伸缩性: NoSQL毕竟还处于发展阶段,也有说它的各种问题的:http://coolshell.c ...
- iOS8 超简单的设置圆角按钮 ImageView等UIView
button.layer.cornerRadius = // 这个值根据你想要的效果可以更改 button.clipsToBounds = true 这种方法不止可以设置按钮,UIView应该都可以设 ...