app/src/main/java/com/example/hellojni/HelloJni.java
/** Called when the activity is first created. */
Called when the activity is first created.
Called when the activity is first created .
@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        TextView  tv = new TextView(this);
        String stringAllocatedByJNI = HelloJniNative.stringFromJNI();
        HelloJniNative.pthreadDemo();
        String storeThis = "Test string to store in the C library.";
        String uninitializedString = HelloJniNative.retrieveTinyString();
        if (0 != uninitializedString.length()) {
            throw new RuntimeException("string should have had zero length");
        }
        HelloJniNative.storeTinyString(storeThis);
        String previouslyStoredString = HelloJniNative.retrieveTinyString();
        String stringToDisplay = "\n" + stringAllocatedByJNI + "\n\n" + previouslyStoredString;
        tv.setText(stringToDisplay);
        setContentView(tv);
    }
@ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ) ; TextView tv = new TextView ( this ) ; String stringAllocatedByJNI = HelloJniNative . stringFromJNI ( ) ; HelloJniNative . pthreadDemo ( ) ; String storeThis = "Test string to store in the C library." ; String uninitializedString = HelloJniNative . retrieveTinyString ( ) ; if ( 0 != uninitializedString . length ( ) ) { throw new RuntimeException ( "string should have had zero length" ) ; } HelloJniNative . storeTinyString ( storeThis ) ; String previouslyStoredString = HelloJniNative . retrieveTinyString ( ) ; String stringToDisplay = "\n" + stringAllocatedByJNI + "\n\n" + previouslyStoredString ; tv . setText ( stringToDisplay ) ; setContentView ( tv ) ; }
Called when the activity is first created.
Called when the activity is first created .
// pretty simplistic app: its entire main window is a single text view // that we create on the fly and then adopt using setContentView(), // instead of having a layout resource // make the TextView instance we are gonna populate // get a String that we are actually // instantiating in the JNI code ... when you use the JNI // environment to instantiate Java objects in C, it's the same as // if we'd instantiated them in Java // pthreads work fine on both iOS and Android // demonstrate storing and retrieving a string from JNI in separate calls - // persistence of information in the C code // set the TextView's text // make this TextView our entire window