从另一个可安装的App启动即时应用程序 [英] Start instant app from another installable App

查看:294
本文介绍了从另一个可安装的App启动即时应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从我拥有的其他可安装应用启动即时应用。这个想法就是这个,一个启动其他即时应用程序的同步应用程序。我已经在google play上发布了我的即时应用程序,它正在完成我在这里完成的测试。

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.

我配置的深层链接是:
https://puzzleinstantapp.firebaseapp.com/main

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

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

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

即时应用程序包名称:
com.lapic.thomas.puzzle

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

但它出错:

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?

谢谢。

推荐答案

由于您的代码段中的 setPackage()而失败。只需发送一个给定URL的VIEW + BROWSABLE意图,它就会启动一个即时应用程序,如果它在这个设备上可用(否则它将回退到浏览器)。

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).

如果你要确保即时应用程序启动,您可以使用此API检查设备上的即时应用是否可用适用于Android的Google API
文档: - 启动即时应用的API

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

这篇关于从另一个可安装的App启动即时应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆