android常用方法

获取应用安装来源

    private String getAppInstaller(Context context, String packageName) {
        return context.getPackageManager().getInstallerPackageName(packageName);
    }

判断是否系统应用

在/system/app 或者 /system/priv-app目录下的应用。

    public boolean isSystem(Context context, String packageName) {
        try {
            PackageManager packageManager = context.getPackageManager();
            ApplicationInfo appInfo = packageManager.getApplicationInfo(packageName, 0);
            if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
                return true;
            }
            if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
                return true;
            }
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
        return false;
    }

获取屏幕的高度

包括状态栏和导航栏高度。

    public int getFullHeight(){
        WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        windowManager.getDefaultDisplay().getRealMetrics(metrics);
        int fullWindowHeight = metrics.heightPixels;
        return fullWindowHeight;
    }

获取导航栏的高度

    public int getNavigationBarHeight() {
        Context context = this;
        int result = 0;
        int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

获取状态栏的高度

public int getStatusBarHeight(Context context) {
    int statusBarHeight = 0;
    Resources resources = context.getResources();
    int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        statusBarHeight = resources.getDimensionPixelSize(resourceId);
    }
    return statusBarHeight;
}

其他

在网上看到一篇总结的非常全面的工具类。
android常用工具类

你可能感兴趣的:(android)