Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I get the device DPI from native code

We are developing an Android application based on our existing native library. We have a set JNI classes to expose the native APIs.The native library has a DPI global static variable need to be initialized in the this block:

JNIEXPORT jint JNICALL JNI_OnLoad( JavaVM *vm, void *pvt ) {
 ...
}

The question is that how do I get the device DPI on native level. I know that on Java level you can easily get the property by WindowManager once activity is up. Is it possible to get the property before my activity startup?

Appreciate your help.

like image 552
Peter Young Avatar asked Nov 23 '25 01:11

Peter Young


1 Answers

As I remember, you need to have Context to get DPI, but in native code you have not. I recommend you to add a native method which will be called in Application.onCreate and set your native variable. This will be done not in JNI_OnLoad but before any activity starts.

like image 162
Ihar Avatar answered Nov 28 '25 07:11

Ihar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!