2013-05-03 12 views
0

登錄我試圖亂舞整合到一個NativeActivity的(沒有基於java的代碼)的Android應用程序,和我沒有任何成功。我已經建立了一個基於Java的妹妹小雪測試的活動,效果很好,但本地電話似乎是通過亂舞Android的事件不是通過NDK登錄,但通過Java

在manifest文件中沒有得到的,我有權限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
package com.myTest.flurry; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 

import com.flurry.android.FlurryAgent; 

public class FlurryActivity extends Activity { 

    //code has correct api key here 
    private static final String appKey = "xxxxxxxxxxxxxxxxxxxx"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_flurry); 
     FlurryAgent.onStartSession(this, appKey); 
     FlurryAgent.setLogEnabled(true); 
     FlurryAgent.setLogLevel(Log.VERBOSE); 
     FlurryAgent.setLogEvents(true); 
     FlurryAgent.logEvent("Starting up!"); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     FlurryAgent.onEndSession(this); 
    } 
} 

它只是足以讓事件出現亂舞網站上。然而,呼籲通過我的本地代碼相同的功能時,該事件不會顯示

#include "flurry.h" 
#include <jni.h> 
#include <android/native_activity.h> 
#include <android_native_app_glue.h> 
#include "log.h" 

JNIEnv *GetEnv(JavaVM *jvm); 
void DetachEnv(JavaVM *jvm); 

Flurry::Flurry() 
    : state_(0) { 
} 

jclass Flurry::GetFlurryAgentClass(JNIEnv *env) { 
    // Extract the FlurryAgent class 
    jobject nativeActivity = state_->activity->clazz; 
    jclass acl = env->GetObjectClass(nativeActivity); 
    // Get the classloader 
    jmethodID getClassLoader = env->GetMethodID(acl, "getClassLoader", "()Ljava/lang/ClassLoader;"); 
    jobject cls = env->CallObjectMethod(nativeActivity, getClassLoader); 
    jclass classLoader = env->FindClass("java/lang/ClassLoader"); 
    // find the loadclass function 
    jmethodID findClass = env->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;"); 
    // Run the loadClass function for the flurry agent 
    jstring strClassName = env->NewStringUTF("com/flurry/android/FlurryAgent"); 
    const char *path = env->GetStringUTFChars(strClassName, 0); 
    // The following line will crash the app because it can't find the class files 
    jclass flurry_agent_class = (jclass)(env->CallObjectMethod(cls, findClass, strClassName)); 
    env->DeleteLocalRef(strClassName); 
    if (env->ExceptionCheck() || flurry_agent_class == 0) { 
    Log("Failed to load flurry class!"); 
    env->ExceptionDescribe(); 
    env->ExceptionClear(); 
    return 0; 
    } 
    return flurry_agent_class; 
} 

void Flurry::SetStateAndStartSession(android_app *state) { 
    state_ = state; 
    JNIEnv *env = GetEnv(state->activity->vm); 
    jclass flurry_agent_class = GetFlurryAgentClass(env); 
    if (flurry_agent_class == 0) { 
    return; 
    } 
    // Start session 
    jmethodID onStartSession_method = env->GetStaticMethodID(flurry_agent_class, "onStartSession", "(Landroid/content/Context;Ljava/lang/String;)V"); 
    if (onStartSession_method) { 
    jstring api_key_string = env->NewStringUTF("xxxxxxxxxxxxxxxxxxxx"); 
    Log("Starting native flurry"); 
    env->CallStaticVoidMethod(flurry_agent_class, onStartSession_method, state->activity->clazz, api_key_string); 
    env->DeleteLocalRef(api_key_string); 
    } 
    // Set logging 
    jmethodID setLogEnabled_method = env->GetStaticMethodID(flurry_agent_class, "setLogEnabled", "(Z)V"); 
    if (setLogEnabled_method) { 
    env->CallStaticVoidMethod(flurry_agent_class, setLogEnabled_method, JNI_TRUE); 
    } 
    // Set logging 
    jmethodID setLogLevel_method = env->GetStaticMethodID(flurry_agent_class, "setLogLevel", "(I)V"); 
    if (setLogLevel_method) { 
    env->CallStaticVoidMethod(flurry_agent_class, setLogLevel_method, 2); 
    } 
    // Set events logging 
    jmethodID setLogEvents_method = env->GetStaticMethodID(flurry_agent_class, "setLogEvents", "(Z)V"); 
    if (setLogEvents_method) { 
    env->CallStaticVoidMethod(flurry_agent_class, setLogEvents_method, JNI_TRUE); 
    } 
    // Set https 
    jmethodID setUseHttps_method = env->GetStaticMethodID(flurry_agent_class, "setUseHttps", "(Z)V"); 
    if (setUseHttps_method) { 
    env->CallStaticVoidMethod(flurry_agent_class, setUseHttps_method, JNI_TRUE); 
    } 
    // Send the event 
    const char *eventname = "Native interface start"; 
    jmethodID logEvent_method = env->GetStaticMethodID(flurry_agent_class, "logEvent", "(Ljava/lang/String;)V"); 
    if (logEvent_method) { 
    Log("Sending Flurry Event: %s", eventname); 
    jstring event_name = env->NewStringUTF(eventname); 
    env->CallStaticVoidMethod(flurry_agent_class, logEvent_method, state_->activity->clazz, event_name); 
    env->DeleteLocalRef(event_name); 
    } 
    //LogEvent("Starting up native interface"); 
} 

void Flurry::EndSession() { 
    // End session 
    JNIEnv *env = GetEnv(state_->activity->vm); 
    jclass flurry_agent_class = GetFlurryAgentClass(env); 
    if (flurry_agent_class == 0) { 
    return; 
    } 
    jmethodID onEndSession_method = env->GetStaticMethodID(flurry_agent_class, "onEndSession", "(Landroid/content/Context;)V"); 
    if (onEndSession_method) { 
    Log("Ending flurry"); 
    env->CallStaticVoidMethod(flurry_agent_class, onEndSession_method, state_->activity->clazz); 
    } 
} 

的輸出日誌亂舞來電似乎與基於Java的電話一致:

示例日誌輸出:

I/FlurryAgent(25878): Agent cache file doesn't exist. 
I/MyApp(25878): Starting native flurry 
I/MyApp(25878): Sending Flurry Event: Native interface start 
D/FlurryAgent(25878): generating report 
D/FlurryAgent(25878): Sending report to: https://data.flurry.com/aap.do 
D/FlurryAgent(25878): Report successful 
D/FlurryAgent(25878): Done sending initial agent report 
D/FlurryAgent(25878): Ending session 

我完全茫然,爲什麼從Java事件被顯示出來,而本土的呼叫和事件都沒有。請幫忙?由於

回答

2

看來,您所呼叫的LOGEVENT方法與錯誤的參數:

env->CallStaticVoidMethod(flurry_agent_class, logEvent_method, state_->activity->clazz, event_name); 

應該

env->CallStaticVoidMethod(flurry_agent_class, logEvent_method, event_name); 

因爲FlurryAgent.logEvent方法只需要一個字符串,而不是一個上下文。