我找到了
http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks(int)
检索当前应用程序的baseActivity或topActivity。然后我就能够获得Activity的资源对象。
但是, Lollipop
中的第三方应用程序不再可以使用api答案 0 :(得分:4)
您可以使用PackageManager.getLaunchIntentForPackage
检索应用程序的启动器Intent
,PackageManager.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
属性。
以下是一些例子:
联系人强>
播放音乐
<强>的Gmail 强>