获取正在运行的任务的Android类别信息

发布于 2024-12-27 18:31:27 字数 295 浏览 3 评论 0原文

在Android中,我可以使用如下代码获取正在运行的任务和活动:

(ActivityManager.RunningTaskInfo) this.mActivityManager.getRunningTasks()

然后我可以获取包名称,类名称等。

我的问题是是否有办法从中获取“类别”信息?或者任何人都可以提出替代解决方案?例如,当我启动一个应用程序时,类别是

cat=[android.intent.category.LAUNCHER] 

In Android, I can get the running tasks and activities using code like tis:

(ActivityManager.RunningTaskInfo) this.mActivityManager.getRunningTasks()

Then I can get the package name, class name etc.

My question is that whether there is a way to get the "category" information from it? Or anyone can suggest an alternative solution? for example, when I launch an app, the category is

cat=[android.intent.category.LAUNCHER] 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忘东忘西忘不掉你 2025-01-03 18:31:27

我不太确定你的意思,但也许以下就是你想要的:通过调用 getIntent() 检索活动中的意图,并在 Intent 对象上调用方法 getCategories( )。此方法将返回一个包含类别字符串的集合。

I am not really sure what you mean, but maybe the following is what you want: retrieve the intent in your activity by calling getIntent(), and on the Intent object call the method getCategories(). This method will return a set with category strings.

可遇━不可求 2025-01-03 18:31:27

我不太确定这是否能解决您的问题,但您可以使用 ActivityManager 的 getRecentTasks(int, int) API 用于获取有关最近任务的信息。它返回 ActivityManager.RecentTaskInfo 对象的列表,其中包含信息关于用于启动任务的意图。您还可以使用 RecentTaskInfo.id 字段将任务与正在运行的任务进行映射。

I am not really sure, whether this would solve your problem, but you may use ActivityManager's getRecentTasks(int, int) API to get information about the recent tasks. It returns a list of ActivityManager.RecentTaskInfo objects, which have information about the intent used to launch the task. You can also use RecentTaskInfo.id field to map the task with the running tasks.

北城挽邺 2025-01-03 18:31:27

我没有找到直接的方法,但这可能会有所帮助:

Intent ApplicationIntent = new Intent(Intent.ACTION_MAIN);
ApplicationIntent.addCategory(Intent.CATEGORY_LAUNCHER);  

List<ResolveInfo> LauncherResolves = packageManager.queryIntentActivities(ApplicationIntent, 0);

List<String> AppsList = new ArrayList<String>();

for (int i = 0; i < LauncherResolves.size(); i++)
    AppsList.add(LauncherResolves.get(i).activityInfo.name);

List<RunningTaskInfo> runningAppInfo = activityManager.getRunningTasks(10);
for (int i = 0; i < runningAppInfo.size(); i++)
{
    if (AppsList.contains(runningAppInfo.get(i).topActivity.getClassName()))
    {
        // Do what ever you want
    }
}

I found no direct way of doing it, but this may help:

Intent ApplicationIntent = new Intent(Intent.ACTION_MAIN);
ApplicationIntent.addCategory(Intent.CATEGORY_LAUNCHER);  

List<ResolveInfo> LauncherResolves = packageManager.queryIntentActivities(ApplicationIntent, 0);

List<String> AppsList = new ArrayList<String>();

for (int i = 0; i < LauncherResolves.size(); i++)
    AppsList.add(LauncherResolves.get(i).activityInfo.name);

List<RunningTaskInfo> runningAppInfo = activityManager.getRunningTasks(10);
for (int i = 0; i < runningAppInfo.size(); i++)
{
    if (AppsList.contains(runningAppInfo.get(i).topActivity.getClassName()))
    {
        // Do what ever you want
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文