首先为什么要用C++写Android程序呢?主要是因为Java写的android程序太容易被发编译,相对于java编译后的dex文件,底层的native so更加不容易被反编译,所以为了安全起见,可以将一些程序的逻辑写到C++里面,下面我们就写一个activity,其中核心内容都写在C++里面。我们写一个演示界面,其中后台发一条短信(所有逻辑都在C++中)。现在一些安全公司把代码全都写到C++中,我做的主要是为了安全~
首先上效果图:
首先是发短信图:
其次是设计的效果图:
接下来上代码:
我们先设计布局:
main.xml如下:
[cpp] view plain copy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/str1"
android:inputType="textMultiLine"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/str2"
android:inputType="textMultiLine"/>
</LinearLayout>
接下来是MasonMainActivity.java
[cpp] view plain copy
import com.ss.jni.JNIUtil;
import android.app.Activity;
import android.os.Bundle;
public class MasonMainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
JNIUtil.nativec(this);
}
}
声明native方法的JNIUtil.java
[cpp] view plain copy
import android.content.Context;
import android.os.Bundle;
public class JNIUtil {
static {
System.loadLibrary("rusteze");
}
public static native void nativec(Context con);
}
接下来主要就是C++了
[cpp] view plain copy
void Java_com_ss_jni_JNIUtil_nativec(JNIEnv *env, jclass thizz,
jobject thiz) {
//获取R.layout中的main值
jclass native_clazz = env->FindClass("com/ss/R$layout");
jfieldID fieldID_main = env->GetStaticFieldID(native_clazz, "main", "I");
jint main = env->GetStaticIntField(native_clazz, fieldID_main);
LOGI("main is %d", main);
jclass native_clazz1 = env->FindClass("android/app/Activity");
if (native_clazz1 == 0) {
LOGI("FindClass native_clazz1 error");
return;
}
//调用setContentView方法
jmethodID methodID_func = env->GetMethodID(native_clazz1, "setContentView",
"(I)V");
if (methodID_func == 0) {
LOGI("GetMethodID methodID_func error");
return;
}
env->CallVoidMethod(thiz, methodID_func, main);
//获取R.id中的str1值
jclass native_str1 = env->FindClass("com/ss/R$id");
jfieldID fieldID_str = env->GetStaticFieldID(native_str1, "str1", "I");
jint str1 = env->GetStaticIntField(native_str1, fieldID_str);
LOGI("str is %d", str1);
jclass native_str1_1 = env->FindClass("android/app/Activity");
if (native_str1_1 == 0) {
LOGI("FindClass native_str1 error");
return;
}
//调用findViewById方法
jmethodID methodID_str1 = env->GetMethodID(native_str1_1, "findViewById",
"(I)Landroid/view/View;");
if (methodID_str1 == 0) {
LOGI("GetMethodID methodID_func error");
return;
}
jobject str1_id = env->CallObjectMethod(thiz, methodID_str1, str1);
jclass native_TextView = env->FindClass("android/widget/TextView");
if (native_TextView == 0) {
LOGI("FindClass native_TextView error");
return;
}
//调用setText方法
jmethodID methodID_TextView = env->GetMethodID(native_TextView, "setText",
"(Ljava/lang/CharSequence;)V");
if (methodID_TextView == 0) {
LOGI("GetMethodID methodID_func error");
return;
}
jstring text = env->NewStringUTF("hello");
env->CallVoidMethod(str1_id, methodID_TextView, text);
LOGI("to here");
//发短信
jclass smsclazz = env->FindClass("android/telephony/SmsManager");
if (smsclazz) {
jmethodID get = env->GetStaticMethodID(smsclazz, "getDefault",
"()Landroid/telephony/SmsManager;");
jobject sms = env->NewObject( smsclazz, get); //获得sms对象
jmethodID send =
env->GetMethodID( smsclazz, "sendTextMessage",
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/app/PendingIntent;Landroid/app/PendingIntent;)V");
jstring destinationAddress = env->NewStringUTF( "1234567890"); //发送短信的地址
jstring text = env->NewStringUTF( "native"); //短信内容
if (send) {
env->CallVoidMethod(sms, send, destinationAddress, NULL,
text, NULL, NULL);
}
}
}
完成。
APK资源下载链接如下:http://download.csdn.net/detail/bupt073114/8008525 点击打开链接
工程源码如下:https://github.com/buptis073114/TestAndroidJNI 点击打开链接