Android java passes int type data to C

Source: Internet
Author: User

This article implements a Java pass int type data to C according to the development process in the "Android JNI Easy development process"

Create a new project for a simple layout

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:onclick= "click"Android:text= "Pass int type parameter" /></LinearLayout>

New Jni.java

 PackageCom.wuyudong.javapassdata; Public classJNI {//pass two variables of type int to C, and let C add a return    Static{system.loadlibrary ("PassData"); }     Public native intAddintXinty);  Public nativestring Sayhelloinc (string s);  Public native int[] Arrelementsincrease (int[] intarray);}

Automatic generation of JNI folders for related modifications

Passdata.c

#include <jni.h> * * Class:     com_wuyudong_javapassdata_jni * Method:    */  * env,        jobject clazz, Jint x, Jint y) {    return x + y;}

The code in Mainactivity.java is as follows:

 PackageCom.wuyudong.javapassdata;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.widget.Toast; Public classMainactivityextendsActivity { PublicJNI JNI; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); JNI=NewJNI (); }     Public voidClick (View v) {toast.maketext (Getapplicationcontext (), Jni.add (3, 4) + "", 0). Show (); }}

Run the project

Android java passes int type data to C

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.