是否有可能获得另一种应用程序的原色,如棒棒糖的最新视图

时间:2014-11-25 08:33:31

标签: android android-5.0-lollipop

我找到了

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

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

但是, Lollipop

中的第三方应用程序不再可以使用api

1 个答案:

答案 0 :(得分:4)

您可以使用PackageManager.getLaunchIntentForPackage检索应用程序的启动器IntentPackageManager.getActivityInfo将为其返回ActivityInfo

获得该信息后,您可以创建新的Theme,然后使用PackageManager.getResourcesForApplication中的资源检索找到colorPrimary值所需的attrs。

    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();
    }

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

以下是一些例子:

联系人

contacts

播放音乐

play music

<强>的Gmail

gmail