The Cocos2d-x3.3RC0 calls Android Java layer URI code through JNI to send SMS, cocos2d-x3.3rc0jni

Source: Internet
Author: User

The Cocos2d-x3.3RC0 calls Android Java layer URI code through JNI to send SMS, cocos2d-x3.3rc0jni
1. If Jni is not described in detail, refer to the previous blog 2. directly go to code 1) Java layer and add it directly to AppActivity. java.

Public class AppActivity extends Cocos2dxActivity {public static Activity acty; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); acty = this;} static {System. loadLibrary ("cocos2dcpp");} public static void Share () {new Thread (new Runnable () {@ Overridepublic void run () {// TODO Auto-generated method stubUri uri = Uri. parse ("Maid: 18928475901"); Intent it = new Intent (Intent. ACTION_SENDTO, uri); it. putExtra ("sms_body", "SMS content"); System. out. println ("test"); it. setFlags (Intent. FLAG_ACTIVITY_NEW_TASK); // acty. startActivity (Intent. createChooser (intent, "share"); acty. startActivity (it );}}). start ();}}
2) Jni layer: The. h and. cpp codes of the Jni/hellocpp/test classes are as follows: test. h
# Ifndef TEST_H _ # define TEST_H_extern "C" {// C ++ calls the Java function interface. This method is used in the menuCallback function of HelloWorldScene. // Void showTipDialog (const char * title, const char * msg); void Share () ;}# endif
Test. cpp note that these two pieces of code are modified on the previous code, and some code has nothing to do with this function. Delete it by yourself.
#include "test.h"#include "cocos2d.h"#include "platform/android/jni/JniHelper.h"#include "../../../Classes/JniTest.h"#include <jni.h>#define CLASS_NAME "org/cocos2dx/cpp/JniTestHelper"#define CLASS_NAMENEW "org/cocos2dx/cpp/AppActivity"using namespace cocos2d;extern "C"{void Share(){bool hasMethod;JniMethodInfo t;hasMethod = JniHelper::getStaticMethodInfo(t,CLASS_NAMENEW,"Share","()V");if(hasMethod){log("Share function");if(t.methodID){t.env->CallStaticVoidMethod(t.classID,t.methodID);log("function share() was called");}else{log("function share was not called");}}else{log("function share was not found");}}}
3) C ++ Layer
Void HelloWorld: menuCloseCallback (Ref * pSender) {# if (CC_TARGET_PLATFORM = CC_PLATFORM_ANDROID) // showTipDialog ("exit", "Exit, Really Go? "); Share (); // call tes. the Share () function in cpp. This function calls the Share () function in Java # endif # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0); # endif}
4) Add the test. cpp file path to the mk file. Add the platform judgment in the header file. The Code is as follows:
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include "../proj.android/jni/hellocpp/test.h"#endif



For ANDROID development, how does JAVA call ANDROID-specific NDK projects? with experience in NDK and JNI

After reading your description, I think this so should not include the interface for interacting with java. You need to write another so interface that provides java and the C project through JNI. You should have the header file of this C project. You only need to encapsulate it with JNI.

In android development, how can I call the static method modified with synchronized of java classes in jni?

Is the method signature parameter in the correct format? Is the method static? At the native level, the class names are not separated by A. Dot but by a slash.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.