[工作积累] 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 ...
随机推荐
- php反射应用实例代码
php反射应用示例. 代码如下:<?php function custom(){ } class custom{ public function index(){ } } prin ...
- webshell + xss 猥琐刷某投票
团队成员发来一个投票的地址,需要撸某某网站的一个某某投票,果断看了下,ip限制了,看到post 数据包 额 随便找个大流量shell post 数据 Js代码代码 <script type=&q ...
- mssql 下删除 default 值的Sql写法
FROM Sys.default_constraints a JOIN sys.columns b ON a.parent_object_id = b.object_id AND a.parent_c ...
- AppCan教你从零开始做开发
经常收到类似这样的提问:新手开发APP,要怎么学?我有满屏幕的文档和视频,然而并没有什么卵用,因为我不知道该从哪看起……今天的主要内容是教大家,如何在AppCan移动平台创建应用,引擎插件选择.证书管 ...
- 九度oj 1541 二叉树
原题链接:http://ac.jobdu.com/problem.php?pid=1541 简答题如下: #include<algorithm> #include<iostream& ...
- Oracle 12c 数据库中scott用户不存在的解决方法
-- 使用超级管理员登录CONN sys/change_on_install AS SYSDBA ;-- 创建c##scott用户CREATE USER c##scott IDENTIFIED BY ...
- 条款2:尽量以const、enum、inline替换#define
1> 以const替换#define • 比如用const double Ratio = 1.653替换#define RATIO 1.653 因为宏定义在预处理阶段就会被替换成其所指代的内容, ...
- Chr()和chrb()的含义(转)
http://blog.csdn.net/cunxiyuan108/article/details/5989701 Chr(charcode) 必要的 charcode 参数是一个用来识别某字符的 L ...
- Oracle把两个空格以上的空格,替换为两个空格
substr( ,instr(,)),)) ) 解释如下: 1. 去掉原字串左右的空格的字符(STR),2.查找STR中空格出现二次的位置(LOC),3.从STR中的第一位到LOC-1截取STR||L ...
- c语言中static 用法总结(转)
惨痛教训: 假设在test.h中定义了一个static bool g_test=false; 若test1.c和test2.c都包含test.h,则test1.c和test2.c分别生成两份g_tes ...