我正在构建一项服务,将已安装应用程序列表从 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 的活动 { 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);
}
}