android通过C获取屏幕大小

android get screen size via C(android通过C获取屏幕大小)
本文介绍了android通过C获取屏幕大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想通过 c 获得屏幕尺寸.我知道 jni 支持从 c 调用 java.但是有没有人知道另一种方法?我的意思是从低级模块获取屏幕大小而不调用 java.

i want to get screen size via c. i know that jni support calling java from c. but is there any person who knows another method? i mean get screen size from lowlevel modules without calling java.

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

public class MainActivity extends Activity
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    Log.v("getWidth", Integer.toString(getWindowManager().getDefaultDisplay().getWidth()));
    Log.v("getHeight", Integer.toString(getWindowManager().getDefaultDisplay().getHeight()));
    }
}

我认为/dev/graphics 与我的问题有关

i think /dev/graphics is related to my questions

推荐答案

是的,你可以从/dev/graphics/fb0 获取屏幕分辨率,但是(至少在我的手机上),读取权限仅限于 root 和用户在图形"组中.无论如何,您都可以这样做(为了清楚起见,删除了错误检查):

Yes, you can get the screen resolution from /dev/graphics/fb0, but (at least on my phone), read access is restricted to root and users in the 'graphics' group. At any rate, you can do something like this (error checking removed for clarity):

// ... other standard includes ...
#include <sys/ioctl.h>
#include <linux/fb.h>

//...

struct fb_var_screeninfo fb_var;
int fd = open("/dev/graphics/fb0", O_RDONLY);
ioctl(fd, FBIOGET_VSCREENINFO, &fb_var);
close(fd);
// screen size will be in fb_var.xres and fb_var.yres

我不确定这些是否被视为公共"NDK 接口,因为我不知道 Google 是否认为Android 在 Linux 上运行并使用 Linux Framebuffer 接口"是公共 API 的一部分,但它似乎确实有效.

I'm not sure if these are considered "public" NDK interfaces, since I don't know if Google considers "Android runs on Linux and uses the Linux Framebuffer interface" to be a part of the public API, but it does appear to work.

如果您确实想确保它是可移植的,您可能应该有一个调用 Java WindowManager API 的 JNI 回退.

If you do want to make sure it's portable, you should probably have a JNI fallback that calls into the Java WindowManager API.

这篇关于android通过C获取屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reliable implementation of PBKDF2-HMAC-SHA256 for JAVA(PBKDF2-HMAC-SHA256 for JAVA 的可靠实现)
Correct way to sign and verify signature using bouncycastle(使用 bouncycastle 签名和验证签名的正确方法)
Creating RSA Public Key From String(从字符串创建 RSA 公钥)
Why java.security.NoSuchProviderException No such provider: BC?(为什么 java.security.NoSuchProviderException 没有这样的提供者:BC?)
Generating X509 Certificate using Bouncy Castle Java(使用 Bouncy Castle Java 生成 X509 证书)
How can I get a PublicKey object from EC public key bytes?(如何从 EC 公钥字节中获取 PublicKey 对象?)