3

I am trying to get running activity name (Foreground activity name) from package name as you all know that we can get currently running activity name
via this line

mActivityManager.getRunningTasks(1).get(0).topActivity.getClassName()

but this work only bellow android 5.0 i need same thing in above 5.0

i am able to get current package name but not activity

1 Answer 1

6

security is enhanced from android Lollipop; see this article for more details: getAppTask

You could get foreground activities via UsageStats class; first, you have to add this permission in your manifest:

<uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"
                                    tools:ignore="ProtectedPermissions"/>

and this declaration:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="your_application_package">

This will disable warning that permission is system level and will not be granted to third-party apps. User of the device can grant permission through the Settings application.

So, in onCreate() method of your MainActivity, check if user granted this permission

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
        if(!hasPermission()){
            startActivityForResult(
                    new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
                    Constant.MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS);
        }
    }

And if no, ask user to enable it:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Constant.MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS){
        if (!hasPermission()){
            startActivityForResult(
                    new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS),
                    Constant.MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS);
        }
    }
}
private boolean hasPermission() {
    AppOpsManager appOps = (AppOpsManager)
            getSystemService(Context.APP_OPS_SERVICE);
    int mode = 0;
    if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.KITKAT) {
        mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
                android.os.Process.myUid(), getPackageName());
    }
    return mode == AppOpsManager.MODE_ALLOWED;
}

Finally, here's a snippet to put in a background service, that gives you package name of current foreground activites:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private String getUsageStatsForegroundActivityName() {


    UsageStatsManager mUsageStatsManager = (UsageStatsManager) MyApplication.getInstance().getSystemService(Context.USAGE_STATS_SERVICE);
    long endTime = System.currentTimeMillis();
    long beginTime = endTime - 1000 * 60;

    // result
    String topActivity = null;

    // We get usage stats for the last minute
    List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, beginTime, endTime);

    // Sort the stats by the last time used
    if (stats != null) {
        SortedMap<Long, UsageStats> mySortedMap = new TreeMap<Long, UsageStats>();
        for (UsageStats usageStats : stats) {
            mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
        }
        if (mySortedMap != null && !mySortedMap.isEmpty()) {
            topActivity = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
            logger.info("topActivity: " + topActivity);
        }
    }
    if (topActivity != null)
        return topActivity;
    else
        return Constant.ACTIVITY_NOT_FOUND;

}
Sign up to request clarification or add additional context in comments.

4 Comments

how to use hasPermission()
I updated my answer and added hasPermission() method
Could you post whole class. I found some error from your class as Constant.MY_PERMISSIONS_REQUEST_PACKAGE_USAGE_STATS
Please note that the user will be required to provide access to usage stat on the device settings at Security->Apps with usage access. Do you know how to automatic turn on this mode. If not turn on the setting, your code will not works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.