getLaunchIntentForPackage is null for some apps

后端 未结 3 1576
无人及你
无人及你 2020-12-01 14:08

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

相关标签:
3条回答
  • 2020-12-01 14:53

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

    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.

    0 讨论(0)
  • 2020-12-01 14:57
        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
                    }
    
    0 讨论(0)
  • 2020-12-01 15:01

    I got the same error when calling getLaunchIntentForPackage(packageName). It was fixed by adding this in the launcher activity's intent-filter tag in the manifest file.

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

    When creating a new TV application in Android Studio, it didn't have the above as the default, instead it has this as the default in the launcher activity's intent-filter tag in the manifest file.

    <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
    
    0 讨论(0)
提交回复
热议问题