是否有可能获得另一个应用程序的主要颜色,例如棒棒糖的最近视图

dar*_*iny 6 android android-5.0-lollipop

我发现

http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks(int)

检索当前应用程序的 baseActivity 或 topActivity。然后我就能够获取活动的资源对象。

但是,该 API 不再可供Lollipop中的第三方应用程序使用

adn*_*eal 6

您可以使用并返回Intent应用程序的启动器。PackageManager.getLaunchIntentForPackagePackageManager.getActivityInfoActivityInfo

获得该信息后,您可以创建一个新的Theme,然后使用其中的资源PackageManager.getResourcesForApplication来检索查找该colorPrimary值所需的属性。

    try {
        final PackageManager pm = getPackageManager();

        // The package name of the app you want to receive resources from
        final String appPackageName = ...;
        // Retrieve the Resources from the app
        final Resources res = pm.getResourcesForApplication(appPackageName);
        // Create the attribute set used to get the colorPrimary color
        final int[] attrs = new int[] {
                /** AppCompat attr */
                res.getIdentifier("colorPrimary", "attr", appPackageName),
                /** Framework attr */
                android.R.attr.colorPrimary
        };

        // Create a new Theme and apply the style from the launcher Activity
        final Theme theme = res.newTheme();
        final ComponentName cn = pm.getLaunchIntentForPackage(appPackageName).getComponent();
        theme.applyStyle(pm.getActivityInfo(cn, 0).theme, false);

        // Obtain the colorPrimary color from the attrs
        TypedArray a = theme.obtainStyledAttributes(attrs);
        // Do something with the color
        final int colorPrimary = a.getColor(0, a.getColor(1, Color.WHITE));
        // Make sure you recycle the TypedArray
        a.recycle();
        a = null;
    } catch (final NameNotFoundException e) {
        e.printStackTrace();
    }
Run Code Online (Sandbox Code Playgroud)

需要注意的是,启动器Activity可能不包含theme属性,在这种情况下您可以考虑使用PackageManager.getApplicationInfo,但不能保证标签也Application包含属性。theme

这里有些例子:

联系方式

联系人

播放音乐

播放音乐

邮箱

谷歌邮箱