2015-05-25 11 views
8

मैं एक ऐसी सेवा तैयार कर रहा हूं जो एंड्रॉइड टीवी या फायर टीवी से इंस्टॉल किए गए ऐप्स की एक मोबाइल फोन पर भेजता है। फोन फिर ऐप के पैकेज नाम को वापस भेजता है जिसे वह लॉन्च करना चाहता है और सेवा इसे लॉन्च करती है।getLaunchIntentForPackage कुछ ऐप्स के लिए शून्य है

इस कोड है कि सूची बनाता है

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

आग टीवी सब कुछ पर से कई के लिए पूरी तरह से है, लेकिन एंड्रॉयड टीवी पर आमादा काम करता है ऐप्स हमेशा शून्य है। ये तो बहुत कम हैं।

  • 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 प्राप्त कोई गतिविधि नहीं आशय {संभालने के लिए पाया बिल्ली = [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); 
    } 
} 
+0

ध्यान रखें कि एंड्रॉइड टीवी पर, 'LEANBACK_LAUNCHER' लांचर की बजाय लॉन्चर की श्रेणी है, क्योंकि यह फोन और टैबलेट पर है। मेरा अनुमान है कि उन ऐप्स में 'लॉन्चर' गतिविधि नहीं है और 'getLaunchIntentForPackage() 'केवल' LAUNCHER' के लिए काम करता है, न कि 'LEANBACK_LAUNCHER'। – CommonsWare

+0

मैंने इरादे के लिए मैन्युअल रूप से पैकेज नाम सेट करने और 'android.intent.category.LEANBACK_LAUNCHER' ​​श्रेणी जोड़ने की कोशिश की लेकिन यह अभी भी काम नहीं करता है। मुझे यकीन है कि नेटफ्लिक्स में 'LEANBACK_LAUNCHER' है। यदि यह मायने रखता है तो मैं एसडीके संस्करण 17-21 को लक्षित कर रहा हूं। –

+1

"मैंने इरादे के लिए पैकेज नाम सेट करने और android.intent.category.LEANBACK_LAUNCHER श्रेणी जोड़ने की कोशिश की लेकिन यह अभी भी काम नहीं करता है" - मुझे नहीं पता कि आपके मौजूदा कोड के संदर्भ में इसका मतलब क्या है । सभी 'LEANBACK_LAUNCHER' गतिविधियों को खोजने के लिए 'queryIntentActivities()' का उपयोग करने के लिए आपका स्वागत है। वह (हालांकि 'लांचर' के साथ) होम स्क्रीन क्या करती है, 'getLaunchIntentForPackage() 'का उपयोग न करें)। होम स्क्रीन-स्टाइल लॉन्चर का एक उदाहरण यहां दिया गया है: https://github.com/commonsguy/cw-omnibus/tree/master/Introspection/Launchalot – CommonsWare

उत्तर

7

होम स्क्रीन शैली लांचर बनाने के लिए, क्षुधा के लिए मत देखो और फिर पाने के लांच करने के लिए प्रत्येक के लिए Intents प्रयास करें। पर 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 टीवी है उपयोग करता है, और टीवी-विशिष्ट एपीके में नियमित LAUNCHER गतिविधि नहीं हो सकती है, या सबसे अच्छी तरह से एक टीवी पर उपयोग के लिए आदर्श नहीं है।

+0

मैंने आपके उत्तर को संशोधित करने का प्रयास किया, लेकिन यह असफल रहा। क्या आप एक उत्तर का सुझाव दे सकते हैं? http://stackoverflow.com/questions/38856092/why-doesnt-ui-automator-launch-activity – likejiujitsu

0
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

आपके कोड की क्या अपेक्षा की जाती है इसकी एक व्याख्या ... –

संबंधित मुद्दे