Start instant app from another installable App

2020-04-22 10:10发布

I need to start an Instant App from another installable app I own. The idea is really this, a sync app that starts other instant apps. I've already posted my instant app on google play and it's working through the tests I've done here.

My deeplink configured is: https://puzzleinstantapp.firebaseapp.com/main

My Assetlinks.json: https://puzzleinstantapp.firebaseapp.com/.well-known/assetlinks.json

Instant app package name: com.lapic.thomas.puzzle

I tried to run it as follows:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://puzzleinstantapp.firebaseapp.com/main"));
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setPackage(getPackageName());
startActivity(intent);

But it gives an error:

04-06 16:57:23.747 30233-30233/com.example.thomas.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.thomas.myapplication, PID: 30233
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thomas.myapplication/com.example.thomas.myapplication.MainActivity}: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://puzzleinstantapp.firebaseapp.com/... pkg=com.lapic.thomas.puzzle }
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
    at android.app.ActivityThread.access$900(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1391)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:234)
    at android.app.ActivityThread.main(ActivityThread.java:5526)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://puzzleinstantapp.firebaseapp.com/... pkg=com.lapic.thomas.puzzle }
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1805)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514)
    at android.app.Activity.startActivityForResult(Activity.java:3963)
    at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
    at android.app.Activity.startActivityForResult(Activity.java:3924)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
    at android.app.Activity.startActivity(Activity.java:4247)
    at android.app.Activity.startActivity(Activity.java:4215)
    at com.example.thomas.myapplication.MainActivity.onCreate(MainActivity.java:17)
    at android.app.Activity.performCreate(Activity.java:6285)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)

Did I forget to set something up? When I run the Instant App directly from google play it works normally. Does anyone have any idea how I can do to start the instant app from a call from my other app installed?

Thank you.

1条回答
小情绪 Triste *
2楼-- · 2020-04-22 11:14

It fails because of setPackage() in your snippet. Just send a VIEW + BROWSABLE intent for given URL and it will launch an instant app if it's available on this device (it will fall back to browser otherwise).

If you want to guarantee that instant app will launch you can use this API to check if instant app is available on device first Google APIs for Android Documentation:- API for launching instant apps

This will take care of the case where instant app cannot run on device because OS is too old, or because user is opted out, or intant app is unavailable in a given country.

You can also use it to get metadata about the instant app before launching it (such as icon or title).

InstantAppIntentData intentData = InstantApps
    .getLauncher(MainActivity.this)
    .getInstantAppIntentData("https://vimeo.com/148943792", null);
int matchResult = intentData.getMatchResult();
if (matchResult == RESULT_LAUNCH_OK) {
  Log.i(TAG, "Launching instant: " + intentData.getPackageName());
  startActivity(intentData.getIntent());
} else {
  Log.i(TAG, "Match result: " + matchResult);
}

InstantApps
  .getLauncher(MainActivity.this)
  .getInstantAppLaunchData("https://vimeo.com/148943792")
  .addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
      LaunchData data = task.getResult();
      Intent launchIntent = data.getIntent();
      if (launchIntent != null) {
        Log.i(TAG, "App label: " + data.getApplicationLabel());
        Log.i(TAG, "Package: " + data.getPackageName());
        Log.i(TAG, "Icon width: " + data.getApplicationIcon().getWidth());
        startActivity(launchIntent);
      } else {
        Log.i(TAG, "No instant app found");
      }
    } else {
      Log.i(TAG, "Error: " + task.getException());
    }
  });
查看更多
登录 后发表回答