繼承時把Activity改成NativeActivity,這個類別只適用於Android2.3以及NDKr5以上的版本。
Java:
package com.horizon.AndroidHelloWorld;
import com.horizon.AndroidHelloWorld.R;
import android.os.Bundle;
import android.app.NativeActivity;
import android.util.Log;
import android.view.Menu;
public class MyMainNativeActivity extends NativeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("MyMainNativeActivity", "MyMainNativeActivity.onCreate");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public void onPause(){
super.onPause();
Log.d("MyMainNativeActivity", "MyMainNativeActivity.onPause");
}
@Override
public void onResume(){
super.onResume();
Log.d("MyMainNativeActivity", "MyMainNativeActivity.onResume");
}
}
C++:main.cpp
#include <jni.h>
#include <android/log.h>
#include <android_native_app_glue.h>
/**
* This is the main entry point of a native application that is using
* android_native_app_glue. It runs in its own thread, with its own
* event loop for receiving input events and doing other things.
*/
void android_main(struct android_app* state) {
// Make sure glue isn't stripped.
app_dummy();
__android_log_print(ANDROID_LOG_DEBUG, "C++", "NDK:LC: [%s]", "Hello NativeActivity");
while(true){
//do game loop
//__android_log_print(ANDROID_LOG_DEBUG, "C++", "NDK:LC: [%s]", "Game Loop");
}
}
/*#include <stdio.h>
#include <stdlib.h>
#include <jni.h>
int main()
{
JNIEnv *env;
JavaVM *jvm;
JavaVMInitArgs vm_args;
JavaVMOption options[1];
// launch Jvm
options[0].optionString = "-Djava.class.path=."; // add user classes
vm_args.version = JNI_VERSION_1_6; //JDK version.
vm_args.options = options;
vm_args.nOptions = 1;
if( JNI_CreateJavaVM(&jvm, (void*) &env, &vm_args) < 0 )
{
fprintf( stderr , "Launch JVM Error\n" );
exit(1);
}
// find the obj & method
jclass my_class;
jmethodID my_main;
if( !( my_class = (*env)->FindClass( env, "MyJavaClass") ) )
{
fprintf( stderr , "'Class' Not Found\n" );
exit(1);
}
if( !( my_main = (*env)->GetStaticMethodID( env, my_class , "main" , "([Ljava/lang/String;)V" ) ) )
fprintf( stderr , "'main' Not Found\n" );
else // Call main function
(*env)->CallStaticVoidMethod( env, my_class, my_main, NULL);
// finish
(*jvm)->DestroyJavaVM(jvm);
return 0;
}
@ MyJavaClass.java
class MyJavaClass
{
public static void main( String []arg)
{
System.out.println("Hello World");
}
}*/
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := myNative
LOCAL_SRC_FILES += main.cpp
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := android_native_app_glue
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.horizon.AndroidHelloWorld"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:hasCode="true" >
<activity
android:name=".MyMainNativeActivity"
android:label="@string/title_activity_main" >
<meta-data android:name="android.app.lib_name"
android:value="myNative" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Our activity is the built-in NativeActivity framework class.
This will take care of integrating with our NDK code. -->
<!-- <activity android:name="android.app.NativeActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboardHidden">
<meta-data android:name="android.app.lib_name"
android:value="myNative" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>-->
</application>
</manifest>
先至CYGWIN的project目錄下ndk-build把.so檔建立出來後即可執行。
Leave a Reply