安卓应用登陆的时候调用login的方法的时候将相关的用户名和密码等参数传递到C层,再服务器端返回验证的结果判断是否可以跳转

使用多个cpp文件的时候Android.mk需要修改,新增了一个login.cpp,除了新增文件以外,其他的操作流程还是5步进行

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := FunUtils
LOCAL_SRC_FILES := FunUtils.cpp login.cpp
LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

BridgeUtils.java

package com.ldw.hello;

/**
 * author: ldw
 * created on: 2019/10/12 1:05
 * description:
 */
//专门调用jni C++接口的类
public class BridgeUtils {

    private static BridgeUtils instance = null;
    //设置一个单例
    public static BridgeUtils getInstance() {
        if (instance == null) {
            instance = new BridgeUtils();
        }

        return instance;
    }

    //提供一个调用JNI接口的成员方法
    public native void hello_jni();
    public native String hello_jni_string();
    //登录认证jni的login
    public native boolean login(String name, String pass, boolean who);


    //加载cpp给提供的 动态库
    static {
        System.loadLibrary("FunUtils"); //libtestjni.so
    }
}

javah执行javah -jni com.ldw.hello.BridgeUtils以后会生成很多方法的声明,这个时候为了单独处理login方法,可以单独处理一个login.h和login.cpp,这个时候将javah中com_ldw_hello_BridgeUtils.h生成的方法声明单独挑选到login.h(避免重复声明),这样FunUtils.cpp中的方法和Login.cpp中的方法不重复

com_ldw_hello_BridgeUtils.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_ldw_hello_BridgeUtils */

#ifndef _Included_com_ldw_hello_BridgeUtils
#define _Included_com_ldw_hello_BridgeUtils
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_ldw_hello_BridgeUtils
 * Method:    hello_jni
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_ldw_hello_BridgeUtils_hello_1jni
  (JNIEnv *, jobject);

/*
 * Class:     com_ldw_hello_BridgeUtils
 * Method:    hello_jni_string
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_ldw_hello_BridgeUtils_hello_1jni_1string
  (JNIEnv *, jobject);


#ifdef __cplusplus
}
#endif
#endif

login.h

//
// Created by Administrator on 2019/10/13.
//
#include <jni.h>
#ifndef HELLO_LOGIN_H
#define HELLO_LOGIN_H

#define TAG   "JNI"

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Class:     com_ldw_hello_BridgeUtils
 * Method:    login
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z
 */
JNIEXPORT jboolean JNICALL Java_com_ldw_hello_BridgeUtils_login
  (JNIEnv *, jobject, jstring, jstring, jboolean);

#ifdef __cplusplus
}
#endif



#endif //HELLO_LOGIN_H

FunUtils.cpp

#include <jni.h>
#include <android/log.h>
#include <com_ldw_hello_BridgeUtils.h>

void JNI_show()
{
    __android_log_print(ANDROID_LOG_ERROR,"jni_show", "Function from FunUtils.cpp form void funciotn");
    return;
}

#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_ldw_hello_BridgeUtils
 * Method:    hello_jni
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_ldw_hello_BridgeUtils_hello_1jni
  (JNIEnv * env, jobject obj)
  {
       //return env->NewStringUTF("Function from FunUtils.cpp");
       JNI_show();
  }

/*
 * Class:     com_ldw_hello_BridgeUtils
 * Method:    hello_jni_string
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_ldw_hello_BridgeUtils_hello_1jni_1string
(JNIEnv * env, jobject obj)
{
    return env->NewStringUTF("Function from FunUtils.cpp");
}


#ifdef __cplusplus
}
#endif

login.cpp模拟向服务器发送json数据,同时服务器返回一个JNI的数据JNI_TURE,来作为返回值返回到java层,完成登录成功的过程

//
// Created by Administrator on 2019/10/13.
//

#include "login.h"
#include <android/log.h>

/*
 * Class:     com_ldw_hello_BridgeUtils
 * Method:    login
 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z
 */
JNIEXPORT jboolean JNICALL Java_com_ldw_hello_BridgeUtils_login
  (JNIEnv * env, jobject obj, jstring jni_username, jstring jni_password, jboolean jni_isdriver)
  {
    const char *username = env->GetStringUTFChars(jni_username, 0);
    const char *passwd = env->GetStringUTFChars(jni_password, 0);
    const char *isDriver = jni_isdriver == JNI_TRUE?"yes":"no";

    __android_log_print(ANDROID_LOG_ERROR,TAG,"JNI-login: username = %s, passwd = %s, isDriver = %s",
                            username, passwd, isDriver);

    //封装一个数据协议
    /*

     ====给服务端的协议====
    http://ip:port/login [json_data]
    {
      username: "gailun",
      password: "123123",
      driver:   "yes"
    }
    *
    *
    * */
    //(1)封装一个json字符串
    //(2) 想web服务器 发送http请求 其中post数据 json字符串
    //(3) 等待服务器的响应
    /*
     //成功
    {
       result: "ok",
    }
    //失败
    {
       result: "error",
       reason: "why...."
    }
    *
    * */
    //(4) 解析服务器返回的json字符串

    return JNI_TRUE;
  }

LoginActivity.java截取部分代码如下

boolean login_result = true;
//调用jni函数的方法,将用户的信息发送给服务器,服务器返回相关信息
login_result = BridgeUtils.getInstance().login(username, password, isDriver);

//将用户名的信息递交给服务器,服务器返回结果login_result,放在login_result中
if(login_result == true){
	if(isDriver){
		startActivity(new Intent(getApplicationContext(), DriverActivity.class));
	}else{
		startActivity(new Intent(getApplicationContext(), PassengerActivity.class));
	}
	//登录成功
	Log.e(LOG_TAG, "登录成功!!! username=" + username + "|||password=" + password);
}else {
	//登录失败
	Log.e(LOG_TAG, "登录失败!!! username=" + username + "|||password=" + password);
}

完整代码如下:

package com.ldw.hello;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    private TextView tv_username, tv_password;
    private EditText et_name, et_pass;
    private Button bt_login, bt_register;
    private String username, password;
    private CheckBox cb_driver;
    private boolean isDriver = false;
    private String LOG_TAG = "LoginActivity===";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        initUI();



        cb_driver.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    isDriver = true;
                    Log.e(LOG_TAG, "是司机!!! username=" + username + "|||password=" + password);
                }else {
                    isDriver = false;
                    Log.e(LOG_TAG, "是乘客!!! username=" + username + "|||password=" + password);
                }
            }
        });


        bt_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                boolean login_result = true;
                username = et_name.getText().toString();
                password = et_pass.getText().toString();
                if(username == ""){
                    Log.e(LOG_TAG, "username 是空的");
                    Toast.makeText(LoginActivity.this, "username 是空的" , Toast.LENGTH_SHORT).show();
                    return;
                }else if(password == ""){
                    Log.e(LOG_TAG, "password 是空的");
                    Toast.makeText(LoginActivity.this, "password 是空的" , Toast.LENGTH_SHORT).show();
                    return;
                }

                //调用jni函数的方法,将用户的信息发送给服务器,服务器返回相关信息
                login_result = BridgeUtils.getInstance().login(username, password, isDriver);

                //将用户名的信息递交给服务器,服务器返回结果login_result,放在login_result中
                if(login_result == true){
                    if(isDriver){
                        startActivity(new Intent(getApplicationContext(), DriverActivity.class));
                    }else{
                        startActivity(new Intent(getApplicationContext(), PassengerActivity.class));
                    }
                    //登录成功
                    Log.e(LOG_TAG, "登录成功!!! username=" + username + "|||password=" + password);
                }else {
                    //登录失败
                    Log.e(LOG_TAG, "登录失败!!! username=" + username + "|||password=" + password);
                }
            }
        });

        bt_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e(LOG_TAG, "用户注册!!! ");
                Intent intent = new Intent();
                intent.setClass(getApplicationContext(), RegisterActivity.class);
                startActivity(intent);
            }
        });




    }

    public void initUI(){
        tv_username = (TextView) findViewById(R.id.tv_username);
        tv_password = (TextView) findViewById(R.id.tv_password);
        bt_login = (Button) findViewById(R.id.bt_login);
        bt_register = (Button) findViewById(R.id.bt_register);
        et_name = (EditText) findViewById(R.id.et_name);
        et_pass = (EditText) findViewById(R.id.et_pass);
        cb_driver = (CheckBox) findViewById(R.id.cb_driver);
        tv_username.setText("用户名:");
        tv_password.setText("密    码:");
        bt_login.setText("登录");
        bt_register.setText("注册");
    }
}

打印如下,中间的是c层的打印结果:

2019-10-13 09:11:59.915 20182-20182/com.ldw.hello E/LoginActivity===: 是司机!!! username=null|||password=null
2019-10-13 09:12:02.241 20182-20182/com.ldw.hello E/JNI: JNI-login: username = nihao, passwd = 12312312312, isDriver = yes
2019-10-13 09:12:02.249 20182-20182/com.ldw.hello E/LoginActivity===: 登录成功!!! username=nihao|||password=12312312312

截图如下:

Android cpp 调用 android log 打印_#endif