Is it possible to get a different main view of the app like watching lollipops on lollipops

I found

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

to get the current base base of the application. Then I managed to get the Activity Resources object .

However, the api is no longer available for third party apps in Lollipop

+3


source to share


1 answer


You can get the launcher Intent

for the app using PackageManager.getLaunchIntentForPackage

and PackageManager.getActivityInfo

will return ActivityInfo

for it.

Once you have this information, you can create a new one Theme

, then use the resources from PackageManager.getResourcesForApplication

to get the attrs needed to find the value 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();
    }

      

One caveat is that the launcher Activity

cannot contain the attribute Theme

, in which case you can use PackageManager.getApplicationInfo

, but there is no guarantee that the tag Application

contains the attribute Theme

.

Here are some examples:

Contacts



contacts

Play music

play music

Gmail

gmail

+4


source







All Articles