简体   繁体   English

如何添加<queries>清单中的标签?</queries>

[英]How to add the <queries> tag in the manifest?

I have this code and I was told that for Android 11 I need to add the queries tag in manifest:我有这段代码,有人告诉我,对于 Android 11,我需要在清单中添加queries标签:

    final PackageManager pm = getPackageManager();
    Intent main = new Intent(Intent.ACTION_MAIN, null);
    main.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> packages = pm.queryIntentActivities(main, 0); //get a list of installed apps.

I was told it should be this:有人告诉我应该是这样的:

  <queries>
    <intent>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent>

  </queries>

Problem is I cannot add the <category> tag.问题是我无法添加<category>标签。 Android Studio says that the element is not allowed there. Android Studio 说那里不允许使用该元素。 I can only add the <action> and <data> tags inside <intent> .我只能在<intent>中添加<action><data>标记。 And it seems other people on SO have this problem too.似乎SO上的其他人也有这个问题。 It's strange because Android's own documentation instructs us to use the <category> tag there.这很奇怪,因为 Android 自己的文档指示我们在那里使用<category>标记。

The <queries> section is introduced in API 30 , so be sure you use it only if you target such version upward. <queries>部分是在 API 30 中引入的,因此请确保仅在将此类版本向上定位时才使用它。

It is indeed needed when, for instance, you want to launch some other apps from yours, knowing only their package name.例如,当您想从您的应用程序中启动一些其他应用程序时,确实需要它,只知道它们的 package 名称。 Otherwhise, using the package manager will return you some null when using functions like packageManager.getLaunchIntentForPackage("com.yyy") .否则,在使用类似packageManager.getLaunchIntentForPackage("com.yyy")的函数时,使用 package 管理器会返回一些null You can find more information here: Package visibility in Android 11您可以在此处找到更多信息: Package 在 Android 11 中的可见性

In your case, you can just write the section like this:在您的情况下,您可以像这样编写部分:

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

    ...

    <queries>
        <intent>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent>
    </queries>

    ...

</manifest>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM