C++调用Java(借助JNI技术实现每日登陆的实现)

来源:互联网 发布:三国志13武将数据修改 编辑:程序博客网 时间:2024/06/02 02:19

<1>C++部分(直接调用Java,实现每日登陆功能)

void DayAwardLayer::loginAward(){CCLog("loginAward");#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台 JniMethodInfo minfo;  jobject jobj;  if (JniHelper::getStaticMethodInfo(minfo, "com/qingxue/game/GunStreet",   "getInstance", "()Ljava/lang/Object;"))  {  jobj = minfo.env->CallStaticObjectMethod(minfo.classID, minfo.methodID); if (JniHelper::getMethodInfo(minfo, "com/qingxue/game/GunStreet",  "interval", "()I"))  {  CCLog("jobj start");if(jobj == NULL) {return;}conLoginDays = minfo.env->CallIntMethod(jobj, minfo.methodID);  if(conLoginDays>7) conLoginDays=7;CCLog("jobj  end");}  } #endif for(int i=0 ; i<conLoginDays-1 ; i++){CCObject* obj = itemArray->objectAtIndex(i);CCNode* item = (CCNode*)obj;item->setVisible(false);CCObject* obj2 = blinkArray->objectAtIndex(i);CCNode* bli = (CCNode*)obj2;bli->setVisible(false);}}

总结:当调用的java方法返回值是int时, 用CallIntMethod, 比如判断用户是否安装了微信.调用的返回值为空时, 用CallVoidMethod

<2>Java部分(计算是否是连续登陆,如果不是,还要重新重置第一次登陆时间)

public int interval() {SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");SharedPreferences mySharedPreferences= getSharedPreferences("date",Activity.MODE_PRIVATE); SharedPreferences.Editor editor = mySharedPreferences.edit(); //debug// editor.putBoolean("firstLogin", true);// editor.commit(); if(mySharedPreferences.getBoolean("firstLogin", true)){editor.putBoolean("firstLogin", false);String curDate =  df.format(new Date()).toString();//curDate = "2014-01-09";Log.e("curDate", curDate);editor.putString("firstLoginDate", curDate);editor.putString("preLoginDate", curDate);editor.commit(); }String firstLoginDate = mySharedPreferences.getString("firstLoginDate", "2014-01-11"); String preLoginDate = mySharedPreferences.getString("preLoginDate", "2014-01-11"); String curLoginDate =  df.format(new Date()).toString();   // curLoginDate = "2014-01-31";Log.e("Date:",firstLoginDate+" : "+ preLoginDate + " : "+ curLoginDate);long from=0;try {from = df.parse(firstLoginDate).getTime();} catch (ParseException e){e.printStackTrace();}long betweet = 0;try {betweet = df.parse(preLoginDate).getTime();} catch (ParseException e){e.printStackTrace();}long to=0;try {to = df.parse(curLoginDate).getTime();} catch (ParseException e){e.printStackTrace();}int bwto = (int) ((to - betweet) / TRUEDAY);if(bwto != 1){//editor.putBoolean("firstLogin", true);Log.e("firstLogin:", "间断了");editor.putString("firstLoginDate", curLoginDate);editor.putString("preLoginDate", curLoginDate);editor.commit(); return 1;} int result = (int) ((to - from) / TRUEDAY);Log.e("interval",result+""); editor.putString("preLoginDate", curLoginDate);  editor.commit();     return result+1;}
<3>提示对话框(如公司信息等)

public void aboutDialogShow(){    AlertDialog.Builder builder1 = new AlertDialog.Builder(GunStreet.this);    builder1.setTitle("关于").setMessage("游戏类型:射击枪战\n" +"客服电话: 0371-63788390\n" +"游戏版本: 本游戏的版权由河南青学电子科\n" +"技有限公司所有,游戏中的文字图片等内容\n" +"均为游戏版权所有者的个人态度或立场,炫\n" +"彩公司对此不承担任何法律责任。").setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int id) {dialog.cancel();}}).create().show();}


0 0
原创粉丝点击