Detect Hardware Acceleration at Runtime: Android

后端 未结 2 1737
情歌与酒
情歌与酒 2020-12-09 12:30

Is it possible to consistently detect if an Activity has hardware acceleration enabled when it is created? I\'m worried that users of my library will enable it through the m

相关标签:
2条回答
  • 2020-12-09 13:22

    I'm new in Android so I was stuck even with the clues given in the answer above.. went to search around and found this code somewhere in the sea of Google. Hope it helps someone.

    /**
     * Returns true if the given Activity has hardware acceleration enabled
     * in its manifest, or in its foreground window.
     *
     * TODO(husky): Remove when initialize() is refactored (see TODO there)
     * TODO(dtrainor) This is still used by other classes.  Make sure to pull some version of this
     * out before removing it.
     */
    public static boolean hasHardwareAcceleration(Activity activity) {
        // Has HW acceleration been enabled manually in the current window?
        Window window = activity.getWindow();
        if (window != null) {
            if ((window.getAttributes().flags
                    & WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED) != 0) {
                return true;
            }
        }
    
        // Has HW acceleration been enabled in the manifest?
        try {
            ActivityInfo info = activity.getPackageManager().getActivityInfo(
                    activity.getComponentName(), 0);
            if ((info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0) {
                return true;
            }
        } catch (PackageManager.NameNotFoundException e) {
            Log.e("Chrome", "getActivityInfo(self) should not fail");
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2020-12-09 13:24

    Try FLAG_HARDWARE_ACCELERATED in flags in ActivityInfo for the activity, which you would get from PackageManager via getActivityInfo().

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题