【问题标题】:getLaunchIntentForPackage is null for some appsgetLaunchIntentForPackage 对于某些应用程序为空
【发布时间】:2015-08-07 09:21:13
【问题描述】:

我正在构建一项服务,将已安装应用程序列表从 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:未找到处理 Intent 的 Activity { 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
  • 我尝试手动设置意图的包名称并添加类别android.intent.category.LEANBACK_LAUNCHER,但它仍然不起作用。我确定 Netflix 有一个LEANBACK_LAUNCHER。如果这很重要,我的目标是 SDK 版本 17-21。
  • “我尝试为意图设置包名称并添加类别 android.intent.category.LEANBACK_LAUNCHER 但它仍然不起作用” - 我不知道你的意思在您现有代码的上下文中。欢迎您使用queryIntentActivities() 查找所有LEANBACK_LAUNCHER 活动。那(尽管使用LAUNCHER)是主屏幕的作用,而不是使用getLaunchIntentForPackage())。这是一个示例主屏幕式启动器:github.com/commonsguy/cw-omnibus/tree/master/Introspection/…
  • 您是否尝试过使用getLeanbackLaunchIntentForPackage()
  • 非常感谢编辑 2!我已经构建了一个启动器,但未能从中启动另一个启动器,因为 pm.getLaunchIntentForPackage 返回 null。你的代码在这个问题上就像一个魅力

标签: android android-tv amazon-fire-tv nexus-player


【解决方案1】:

自 Android 11 以来,一些应用程序将不会提供此信息,除非您将 queries 标签添加到 AndroidManifest 中,否则会发生行为变化

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

更多信息请参考here

【讨论】:

  • 这是教程中没有提到的秘密
  • 我在清单中输入了这个文本,现在它给出了一个错误并且它没有被删除
  • 不要这样做!
  • 这实际上对我有用。 “如果您的应用面向 Android 11(API 级别 30)或更高版本,并且需要与自动可见的应用以外的应用交互,请在应用的清单文件中添加 元素。在 元素中,指定其他应用程序包名称。”
【解决方案2】:

要创建一个主屏幕风格的启动器,不要寻找应用程序,然后尝试为每个应用程序获取启动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 活动,或者最多有一个不一定的活动非常适合在电视上使用。

【讨论】:

  • 我尝试修改你的答案,但它失败了。你能建议一个答案吗? stackoverflow.com/questions/38856092/…
  • 非常感谢!我已经构建了一个启动器,但未能从中启动另一个启动器,因为 pm.getLaunchIntentForPackage 返回 null。你的代码在这个问题上就像一个魅力。
  • 为什么要将组件与用于启动活动的意图的动作和类别一起设置(在 onListItemClick() 中)?设置组件将使意图明确,不需要操作或类别。
  • @PiotrŚmietana:我们开始的活动可能是查看操作或类别以确定要做什么。有时,开发人员在&lt;activity&gt; 上有多个&lt;intent-filter&gt; 元素,需要检查传入的Intent 以区分各种选项。因此,如果您创建一个Intent 来匹配活动的&lt;intent-filter&gt; 并设置组件,那么这是最安全的。这样一来,显式的Intent 就可以保证去你想要的地方,但活动会得到它可能需要的隐式操作+类别。
  • 这实际上是 getLaunchIntentForPackage 在幕后所做的:android.googlesource.com/platform/frameworks/base/+/refs/heads/… 唯一的区别是它在 Intent(你称之为 main)上调用 setPackage 以确保查询的意图仅是所需的特定包的意图。
【解决方案3】:

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

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

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

如果您只希望对有限数量的应用使用 Intent,则应改为在 Android 清单文件中使用查询:

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

【讨论】:

    【解决方案4】:

    我在调用getLaunchIntentForPackage(packageName) 时遇到了同样的错误。通过在清单文件的启动器活动的意图过滤器标记中添加此问题已修复。

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

    在 Android Studio 中创建新的 TV 应用程序时,它没有将上述作为默认值,而是在清单文件中启动器活动的 intent-filter 标记中将其作为默认值。

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

    【讨论】:

      【解决方案5】:
          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
                      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-03
        • 1970-01-01
        • 2018-08-09
        • 2020-12-07
        • 1970-01-01
        • 2023-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多