-1

I'm trying to launch Amazon Prime for TV app (https://play.google.com/store/apps/details?id=com.amazon.amazonvideo.livingroom) from within my own Android TV app. Unfortunately, PackageManager.getLaunchIntentForPackage() returns null.

// called inside a Fragment
val intent = activity?.packageManager?.getLaunchIntentForPackage("com.amazon.amazonvideo.livingroom")
startActivity(intent)

when run, it throws

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.migrateExtraStreamToClipData()' on a null object reference

Of course, I do have Amazon Prime for TV installed on my Android TV. Also, I could use this code to succesfully launch other apps like com.google.android.youtube.tv or com.netflix.ninja

There's a similiar question regarding this topic (how launch amazon prime video app from within my android app using intent), but it doesn't solve my problem - solution to OP's problem was to use PackageManager (which I'm already using) and com.amazon.avod.thirdpartyclient, since the app was running on phone/tablet instead of TV

EDIT: Intent.migrateExtraStreamToClipData() on a null object reference does not solve my problem. I have Google Play Services installed on my Android TV, version 11.5.09, while the bug causing the other question's problem was (supposedly) fixed in 9.4.0. Anyway, accepted solution is just about avoiding app crash, not fixing the real problem.

Piotr Śmietana
  • 393
  • 2
  • 19
  • Possible duplicate of [Intent.migrateExtraStreamToClipData() on a null object reference](https://stackoverflow.com/questions/38041230/intent-migrateextrastreamtoclipdata-on-a-null-object-reference) – Krystian G Feb 28 '19 at 10:31
  • @KrystianG I do have Google Play Services installed on my Android TV. Anyway, I get null intents only for this particular package, not every one. – Piotr Śmietana Feb 28 '19 at 10:49

2 Answers2

0

You can open intent in the following way, it should work, as it worked for me

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);`
basavaraj ganagi
  • 61
  • 1
  • 2
  • 6
  • I don't know Amazon Prime launcher activity name, so I can't use setComponent(ComponentName), since ComponentName constructors require component class name (can't be null). The only thing I have is Amazon Prime package name. I've tried to use Intent.setPackage() instead, but now I get android.content.ActivityNotFoundException not only for Amazon Prime, but also for com.netflix.ninja, which was working before. Did you try your code specifically for Amazon Prime for TV, or just for some other app ? – Piotr Śmietana Feb 28 '19 at 13:13
  • Besides, using setComponent() makes intent explicit, so there's no sense in setting action and category too. That's not how Intents are intended to be used (pun intended (this one too)) – Piotr Śmietana Feb 28 '19 at 13:20
  • 1
    This post had helped a while back when i was trying to do similar thing https://stackoverflow.com/a/30446616/10201535 – basavaraj ganagi Feb 28 '19 at 13:50
0

As explained in getLaunchIntentForPackage is null for some apps, the root cause of the problem is that Android TV apps use Intent.CATEGORY_LEANBACK_LAUNCHER instead of Intent.CATEGORY_LAUNCHER, which causes getLaunchIntentForPackage() to return null. Some TV apps do support Intent.CATEGORY_LAUNCHER, though, that's why the problem doesn't apply to every single app.

I've decided to use solution mentioned in comments (instead of accepted one), as it's simplier and requires less code:

val packageName  = "com.amazon.amazonvideo.livingroom"
val pm = activity?.packageManager
val intent = pm?.getLaunchIntentForPackage(packageName) ?: pm?.getLeanbackLaunchIntentForPackage(packageName)
startActivity(intent)

notice getLeanbackLaunchIntentForPackage() used as a fallback mechanism. This way, activity will be started properly both on Android TV and phone/tablet (mind that phone/table and TV often have separate apps for same service, like com.amazon.avod.thirdpartyclient and com.amazon.amazonvideo.livingroom)

Piotr Śmietana
  • 393
  • 2
  • 19