我怎样才能得到这是其他包装顶部第二届活动的包叫什么名字?(how can i get the pac

2019-10-17 11:04发布

在我的情况下,一个应用程序调用一个服务,该服务将inturn开始的活动。 我在这里的问题是获取应用程序包,其中调用该服务。

任何人可以帮助我解决这个问题?

Answer 1:

我从android源码发现这直接和正常工作:)

final ActivityManager am = (ActivityManager)
                getSystemService(Context.ACTIVITY_SERVICE);

        final List<ActivityManager.RecentTaskInfo> recentTasks =
                am.getRecentTasks(3, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
        for (int i = 0, index = 0; i < 3 && (index < 3); ++i) {
            final ActivityManager.RecentTaskInfo recentInfo = recentTasks.get(i);

            Intent intent = new Intent( recentInfo.baseIntent);
            if ( recentInfo.origActivity != null) {
                intent.setComponent( recentInfo.origActivity);
            }

            final PackageManager pm = getPackageManager();
            final ResolveInfo resolveInfo = pm.resolveActivity(intent, 0);
            final ActivityInfo info = resolveInfo.activityInfo;
            final String title = info.loadLabel(pm).toString();

            Log.d("hello","  "+title+" "+info.packageName);
           final ImageView iv = (ImageView)findViewById(R.id.imageView1);
           iv.setImageDrawable(info.loadIcon(pm));
                ++index;
            }


Answer 2:

尝试这个

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
          ComponentName topActivity = tasks.get(1).topActivity;
          if (!topActivity.getPackageName().equals(context.getPackageName())) {
            return true;
          }
        }

它可能对你有用得到第二顶。



文章来源: how can i get the package name of 2nd top activity which is in other package?