对于某些应用,getLaunchIntentForPackage为null [英] getLaunchIntentForPackage is null for some apps

查看:182
本文介绍了对于某些应用,getLaunchIntentForPackage为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a service that sends a list of installed apps from an Android TV or a Fire TV to a mobile phone. The phone then sends back the package name of the app it wants to launch and the service launches it.

这是创建列表的代码

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;
}

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

This is how I launch the apps

public void launchApp(String packageName) {
    PackageManager pm = getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage(packageName);
    startActivity(intent);
}

在Fire TV上,一切正常,但在Android TV上,许多人的意图是的应用始终为null。这些只是少数几个。

On the Fire TV everything works perfectly but on the Android TV the intent for many of the apps is always null. These are just a few.


  • com.haystack.android

  • com.netflix.ninja

  • tv.pluto.android

  • com.bamnetworks.mlbtv

  • com.haystack.android
  • com.netflix.ninja
  • tv.pluto.android
  • com.bamnetworks.mlbtv

但是使用相同的代码,这些应用程序就可以正常工作。

However with the same code, these apps work just fine.


  • com.hulu.livingroomplus

  • com.sling

  • com.frogmind.badland

  • com.songza.tv

  • com.hulu.livingroomplus
  • com.sling
  • com.frogmind.badland
  • com.songza.tv

有人能提供我可能做错了什么的见解吗?

Could anyone provide any insight on what I might be doing wrong?

谢谢!

编辑:
我也尝试过此操作,但出现异常

I've also tried this and I get the exception

android.content。 ActivityNotFoundException:找不到处理意图的活动{cat = [android.intent.category.LEANBACK_LAUNCHER] flg = 0x10000000 pkg = com.netflix.ninja}

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);
    }
}


推荐答案

到创建一个主屏幕样式的启动器,不要查找应用,然后尝试为每个应用启动 Intents 。使用 PackageManager 上的 queryIntentActivities()查找可启动的活动。

To create a home screen-style launcher, don't look for apps and then try to get launch Intents for each. Look for launchable activities, using queryIntentActivities() on PackageManager.

例如,此活动(来自此示例项目 )使用此技术实现了主屏幕样式的启动器:

For example, this activity (from this sample project) implements a home screen-style launcher using this technique:

/***
  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 活动,或者充其量是不一定适合在电视上使用的活动。

On an Android TV device, you should also search for LEANBACK_LAUNCHER activities, as that's what Android TV uses, and TV-specific APKs might not have a regular LAUNCHER activity, or at best have one that is not necessarily ideal for use on a TV.

这篇关于对于某些应用,getLaunchIntentForPackage为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆