赞
踩
1.在Activity中编写native方法
- package com.godv.ccalljava;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Toast;
-
- public class MainActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
-
- public void showToa(View view) {
- toastCallBack();
- }
-
- public native void toastCallBack();
-
- public void showToast(){
- Log.i("godv","更新UI");
- Toast.makeText(this, "godv", Toast.LENGTH_SHORT).show();
- }
- }
2.生成 .h 文件 重写 .c 中的方法
- JNIEXPORT void JNICALL Java_com_godv_ccalljava_MainActivity_toastCallBack
- (JNIEnv * env, jobject instance)
- {
- jclass jclazz = (*env)->FindClass(env,"com/godv/ccalljava/MainActivity");
- jmethodID jids = (*env)->GetMethodID(env,jclazz,"showToast","()V");
- (*env)->CallVoidMethod(env,instance,jids);
- }
3.在activity调用
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。