一些应用的getLaunchIntentForPackage为空

47

我正在开发一个服务,它会将安装在Android TV或Fire TV上的应用列表发送到手机。然后手机会发送要启动的应用程序包名称,该服务将启动它。

这是创建列表的代码。

public List<InstalledApp> GetInstalledApps(boolean isAndroid) {
    PackageManager pm = getPackageManager();
    List<ApplicationInfo> allPackages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    List<InstalledApp> userPackages = new ArrayList<InstalledApp>();

    for (ApplicationInfo packageInfo : allPackages) {

        if (isSystemPackage(packageInfo)) continue;

        InstalledApp app = new InstalledApp();
        app.setPackageName(packageInfo.packageName);
        app.setAppName(pm.getApplicationLabel(packageInfo).toString());
        if (!isAndroid) {
            app.setIcon(pm.getApplicationIcon(packageInfo));
        }
        app.setAccentColor(getAccentColor(pm.getApplicationIcon(packageInfo)));


        userPackages.add(app);
    }

    return userPackages;
}

这是我启动应用程序的方式

public void launchApp(String packageName) {
    PackageManager pm = getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage(packageName);
    startActivity(intent);
}
在Fire TV上一切都很完美,但在Android TV上,许多应用程序的意图始终为空。以下是其中的几个:
  • com.haystack.android
  • com.netflix.ninja
  • tv.pluto.android
  • com.bamnetworks.mlbtv
然而,使用相同的代码,这些应用程序可以正常工作:
  • com.hulu.livingroomplus
  • com.sling
  • com.frogmind.badland
  • com.songza.tv
有人能提供任何关于我可能做错了什么的见解吗?谢谢!
编辑: 我也尝试过这个,但我得到了异常
android.content.ActivityNotFoundException: No Activity found to handle Intent { cat=[android.intent.category.LEANBACK_LAUNCHER] flg=0x10000000 pkg=com.netflix.ninja }
public void launchApp(String packageName) {
    Intent intent = new Intent();
    intent.setPackage(packageName);
    intent.addCategory("android.intent.category.LEANBACK_LAUNCHER");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

编辑 2:

这是适用于我个人的代码:

public void launchApp(String packageName) {
    Intent intent = new Intent();
    intent.setPackage(packageName);

    PackageManager pm = getPackageManager();
    List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
    Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));

    if(resolveInfos.size() > 0) {
        ResolveInfo launchable = resolveInfos.get(0);
        ActivityInfo activity = launchable.activityInfo;
        ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                activity.name);
        Intent i=new Intent(Intent.ACTION_MAIN);

        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
        i.setComponent(name);

        startActivity(i);
    }
}

请注意,对于 Android TV,LEANBACK_LAUNCHER 是启动器的类别,而不是手机和平板电脑上的 LAUNCHER。我猜这些应用程序没有 LAUNCHER 活动,并且 getLaunchIntentForPackage() 只适用于 LAUNCHER,而不是 LEANBACK_LAUNCHER - CommonsWare
1
我尝试为意图设置包名并添加类别android.intent.category.LEANBACK_LAUNCHER,但仍然无法工作。在您现有代码的上下文中,我不知道您的意思。您可以使用queryIntentActivities()查找所有LEANBACK_LAUNCHER活动。这是主屏幕所做的事情(虽然使用LAUNCHER),而不是使用getLaunchIntentForPackage()。这是一个示例类似主屏幕的启动器:https://github.com/commonsguy/cw-omnibus/tree/master/Introspection/Launchalot - CommonsWare
非常感谢。您能把这个变成答案吗?我会接受它的。 - Jeremy Roberts
6
你尝试过使用 getLeanbackLaunchIntentForPackage() 吗?这个方法可以获取 Android 应用的 Leanback 启动意图。 - Sebastiano
1
非常感谢您的第二次编辑!我已经构建了一个启动器,但无法从中启动另一个启动器,因为pm.getLaunchIntentForPackage返回null。您的代码在这个问题上运行得非常好。 - FrankKrumnow
显示剩余2条评论
5个回答

70

自 Android 11 起,某些应用程序在不添加 queries 标签到 AndroidManifest 中时将不提供此信息。

    <queries>
        <package android:name="app.i.want.to.query" />
    </queries>

请参阅此处获取更多信息。


11
这是教程中没有提到的秘密。 - user3561494
我在清单中输入了这个文本,现在它出现了错误并且没有被删除。 - Arty Morris
不要这样做! - Arty Morris
这对我实际上起作用了。 "If your app targets Android 11 (API level 30) or higher and needs to interact with apps other than the ones that are visible automatically, add the <queries> element in your app's manifest file. Within the <queries> element, specify the other apps by package name." - dmukherjee
更多信息 - https://android-developers.googleblog.com/2020/07/preparing-your-build-for-package-visibility-in-android-11.html - Veeresh Charantimath
显示剩余2条评论

26

在Android 11中,您只能使用getInstalledApplications()获得有限数量的应用程序包名称,并且只能通过使用getLaunchIntentForPackage()获取其中一些的意图。

将此权限添加到您的Android清单文件中以获取所有应用程序包名称和意图。

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

如果您只想针对有限数量的应用程序使用意图,那么您应该在Android清单文件中使用查询。

<queries>
    <package android:name="packageName" />
</queries>

25
要创建一个类似于主屏幕的启动器,不要寻找应用程序,然后尝试获取每个应用程序的启动Intents。使用PackageManager上的queryIntentActivities()查找可启动的活动。

例如,这个活动(来自this sample project)使用这种技术实现了一个类似于主屏幕的启动器:
/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
*/

package com.commonsware.android.launchalot;

import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import java.util.Collections;
import java.util.List;

public class Launchalot extends ListActivity {
  AppAdapter adapter=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    PackageManager pm=getPackageManager();
    Intent main=new Intent(Intent.ACTION_MAIN, null);

    main.addCategory(Intent.CATEGORY_LAUNCHER);

    List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);

    Collections.sort(launchables,
                     new ResolveInfo.DisplayNameComparator(pm)); 

    adapter=new AppAdapter(pm, launchables);
    setListAdapter(adapter);
  }

  @Override
  protected void onListItemClick(ListView l, View v,
                                 int position, long id) {
    ResolveInfo launchable=adapter.getItem(position);
    ActivityInfo activity=launchable.activityInfo;
    ComponentName name=new ComponentName(activity.applicationInfo.packageName,
                                         activity.name);
    Intent i=new Intent(Intent.ACTION_MAIN);

    i.addCategory(Intent.CATEGORY_LAUNCHER);
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    i.setComponent(name);

    startActivity(i);    
  }

  class AppAdapter extends ArrayAdapter<ResolveInfo> {
    private PackageManager pm=null;

    AppAdapter(PackageManager pm, List<ResolveInfo> apps) {
      super(Launchalot.this, R.layout.row, apps);
      this.pm=pm;
    }

    @Override
    public View getView(int position, View convertView,
                          ViewGroup parent) {
      if (convertView==null) {
        convertView=newView(parent);
      }

      bindView(position, convertView);

      return(convertView);
    }

    private View newView(ViewGroup parent) {
      return(getLayoutInflater().inflate(R.layout.row, parent, false));
    }

    private void bindView(int position, View row) {
      TextView label=(TextView)row.findViewById(R.id.label);

      label.setText(getItem(position).loadLabel(pm));

      ImageView icon=(ImageView)row.findViewById(R.id.icon);

      icon.setImageDrawable(getItem(position).loadIcon(pm));
    }
  }
}

在Android TV设备上,您还应该搜索LEANBACK_LAUNCHER活动,因为这是Android TV使用的内容,而电视专用的APK可能没有常规的LAUNCHER活动,或者最多只有一个不一定适合在电视上使用的活动。

我尝试了你修改后的答案,但是它失败了。你能否提供一个解决方案?http://stackoverflow.com/questions/38856092/why-doesnt-ui-automator-launch-activity - likejudo
非常感谢!我已经构建了一个启动器,但由于pm.getLaunchIntentForPackage返回null,无法从该启动器启动另一个启动器。您的代码在这个问题上运行得很好。 - FrankKrumnow
为什么在启动活动时(在onListItemClick()中),要将组件与操作和类别一起设置在意图中?设置组件将使意图明确,无需操作或类别。 - Piotr Śmietana
1
@PiotrŚmietana:我们即将开始的活动可能是查看操作或类别以确定要做什么。有时,开发人员在<activity>上有多个<intent-filter>元素,需要检查传入的意图以区分不同的选项。因此,如果您创建一个将与活动的<intent-filter>匹配并设置组件的“Intent”,那么最安全的方法就是这样做。这样,显式的“Intent”保证了它会去到你想要的地方,但活动却得到了它可能需要的隐式操作+类别。 - CommonsWare
2
这实际上就是 getLaunchIntentForPackage 在幕后执行的操作:https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/app/ApplicationPackageManager.java 唯一的区别在于它在 Intent 上调用了 setPackage(你称之为 main),以确保查询的意图仅限于所需的特定包。 - Adam Burley

1

当我调用getLaunchIntentForPackage(packageName)时,我遇到了相同的错误。通过在清单文件中的启动器活动的intent-filter标签中添加以下内容,问题得以解决。

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

在Android Studio中创建新的电视应用程序时,默认情况下不会具有上述内容,而是将其作为清单文件中启动器活动的intent-filter标签的默认值。
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />

-2
    Intent launchIntent = null;

                try{
                        launchIntent = context.getPackageManager().getLeanbackLaunchIntentForPackage(pkgName);
                    } catch (java.lang.NoSuchMethodError e){
                    }

                    if (launchIntent == null) launchIntent = context.getPackageManager().getLaunchIntentForPackage(pkgName);

                if (launchIntent != null)  {
                    launchIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(launchIntent);
                } else {
                   // failure message
                }

好观点,很高兴在这里听到你的回答。我的应用程序是针对Android TV的,一直以来我都在为此苦恼。你解决了我的困惑。谢谢。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接