从NDK读取Settings.Secure - decodeIndirectRef中的无效间接引用

时间:2014-01-17 14:49:22

标签: java android android-ndk android-contentresolver

我正在尝试从NDK读取ANDROID_ID字符串。

编辑:底部的初始问题,这是我现在的位置:

我删除了static并将Jni代码更改为:

jstring JNICALL GEN_FUNCNAME(MyClass_nGetID)(JNIEnv *env,jobject obj
{

jclass  activity = env->GetObjectClass(obj);

jmethodID  mid_getContentResolver =env->GetMethodID(activity,"getContentResolver","()Landroid/content/ContentResolver;");

jobject contentObj =  env->CallObjectMethod(obj, mid_getContentResolver);

if(contentObj == NULL)
    return env->NewStringUTF("method 1 null");

jclass secClass=env->FindClass("android/provider/Settings$Secure");

if(secClass == NULL)
    return env->NewStringUTF("class 2 null");

jmethodID secMid = env->GetStaticMethodID(secClass,"getString","(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;");

if(secMid == NULL)
    return env->NewStringUTF("method 2 null");

jstring jandroid_id = (jstring) env->CallStaticObjectMethod(secClass,secMid,contentObj,"android_id");

if(jandroid_id == NULL)
    return env->NewStringUTF("android id null");

现在第一阶段似乎有效,但我在CallStaticObjectMethod上收到以下错误:

W/dalvikvm(2698): Invalid indirect reference 0x7509bd4c in decodeIndirectRef
I/dalvikvm(2698):   at android.provider.Settings$Secure.getString(Settings.java:-1)
I/dalvikvm(2698):   at mypackage.myclass.nGetID(Native Method)

感谢您的帮助! :)

< --------------------------初始问题----------------- ------------------->

我可以使用以下方法在Java中成功读取它:

final String android_id = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID); 

我在JNI中的函数CallObjectMethod上收到JNI警告和VM崩溃。

有什么我忘了做的吗?

JNI_WARNING:

W/dalvikvm(14911): JNI WARNING: can't call Landroid/content/Context;.getContentResolver on instance of Ljava/lang/Class; (CallObjectMethodV)

VM崩溃日志:

E/dalvikvm(14911): VM aborting
A/libc(14911): Fatal signal 6 (SIGABRT) at 0x00003a3f (code=-6), thread 14911 

我的JNI功能:

jstring GEN_FUNCNAME(MyClass_nGetID)(JNIEnv *env,jobject obj)

{
jclass  act = env->GetObjectClass(obj);
jclass  clsontentResolver = env->FindClass("android/content/Context");


jmethodID  mid_getContentResolver = env->GetMethodID(clsontentResolver,"getContentResolver","()Landroid/content/ContentResolver;");

jobject contentObj = env->CallObjectMethod(act, mid_getContentResolver);

jclass secClass=env->FindClass("android/provider/Settings$Secure");

jmethodID secMid=env->GetStaticMethodID(secClass,"getString","(Landroid/content/ContentResolver;Ljava/lang/String;)Ljava/lang/String;");

jstring jandroid_id= (jstring) env->CallStaticObjectMethod(secClass,secMid,contentObj,"android_id");

return jandroid_id;

}

My Java Main class:

public class MyClass extends Activity {

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

        text = (TextView)findViewById(R.id.text);

        text.setText(nGetID());

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    static { System.loadLibrary("mylib"); }
    public final static native String nGetID();

}

非常感谢!

1 个答案:

答案 0 :(得分:2)

(编辑)

要将字符串传递回Java,您需要使用NewStringUTF。像这样:

jstring jStringParam = getEnv()->NewStringUTF("android_id");
jstring jandroid_id = (jstring) env->CallStaticObjectMethod(secClass,secMid,contentObj,jStringParam);
// Remember to release when done!
getEnv()->DeleteLocalRef(jStringParam);

(初次发行)

jobject obj未附加到实例

public final static native String nGetID();被定义为静态,这意味着它不会“附加”到任何对象实例。

在您的JNI功能中 第jclass act = env->GetObjectClass(obj);行 - 您传入jobject obj

尝试声明nGetID()没有static,这应该可以解决问题。你有什么理由要static吗?