Get foreground activity from package name?

旧街凉风 提交于 2019-12-07 16:28:57

问题


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:


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;

}


来源:https://stackoverflow.com/questions/33710196/get-foreground-activity-from-package-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!