我需要从我拥有的另一个可安装的应用程序启动一个即时应用程序。这个想法就是这个,一个启动其他即时应用程序的同步应用程序。我已经在谷歌播放上发布了我的即时应用程序,它正在完成我在这里完成的测试。
我的深层配置是: https://puzzleinstantapp.firebaseapp.com/main
My Assetlinks.json: https://puzzleinstantapp.firebaseapp.com/.well-known/assetlinks.json
即时应用包名称: com.lapic.thomas.puzzle
我尝试按如下方式运行它:
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)
我忘记了什么吗? 当我直接从谷歌播放运行即时应用程序时,它正常工作。 有没有人知道如何从我安装的其他应用程序的电话启动即时应用程序?
谢谢。
答案 0 :(得分:2)
由于代码段中的setPackage()
而失败。只需发送一个给定URL的VIEW + BROWSABLE意图,如果它在这个设备上可用,它将启动一个即时应用程序(否则它将回退到浏览器)。
如果您想保证即时应用会启动,您可以使用此API检查设备上的即时应用是否可用Google APIs for Android Documentation:- API for launching instant apps
这将解决即时应用程序无法在设备上运行的情况,因为操作系统太旧,或者因为用户选择退出,或者在给定国家/地区无法使用辅助应用程序。
您也可以在启动它之前使用它来获取有关即时应用的元数据(例如图标或标题)。
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());
}
});