gpt4 book ai didi

Flutter 使用包打开深层链接

转载 作者:行者123 更新时间:2023-12-02 18:27:38 25 4
gpt4 key购买 nike

为了让我的用户打开 Spotify 应用程序,我正在使用以下 flutter 包: https://pub.dev/packages/external_app_launcher

当用户单击应用程序中的按钮时,他应该导航到 Spotify 深层链接。如何使用上面的包实现它?

深层链接应该是这样的: https://open.spotify.com/playlist/37i9dQZF1DWX5ZOsG2Ogi1?si=41c6a392cf7d4a2b

但是,我目前的实现如下:

await LaunchApp.openApp(
androidPackageName: 'com.spotify.music',
iosUrlScheme: 'spotify:',
);

我该怎么做?

最佳答案

您的包用于打开应用程序而不是打开深层链接。深层链接基本上只是打开一个 URL,如果可用,该 URL 会将您重定向到其关联的应用程序。

所以要打开深层链接,您应该使用包 url_launcher并使用它启动您的 Spotify URL。

这是一个关于 Android 的教程:

第 1 步:向您的 AndroidManifest 添加权限

您将需要添加权限 QUERY_ALL_PACKAGES,以便您可以使用 canLaunch 验证 Spotify 应用程序在手机上是否可用。

  • android/app/src/debug/AndroidManifest.xml(如果您也希望它在 Debug模式下工作)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.so_tests">
<!-- Flutter needs it to communicate with the running application
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET"/>

<!-- Add this line -->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
</manifest>
  • android/app/src/main/AndroidManifest.xml(用于发布)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.so_tests">
<uses-permission android:name="android.permission.INTERNET"/>

<!-- Add this line -->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
<application
android:label="so_tests"
android:icon="@mipmap/ic_launcher">

<!-- TODO: Add your Google Maps API key here -->
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="YOUR-KEY-HERE"/>

<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
while the Flutter UI initializes. After that, this theme continues
to determine the Window background behind the Flutter UI. -->
<meta-data
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<!-- Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>

第 2 步:使用 canLaunchlaunch

这是一个基本的代码示例:

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: () async {
final spotifyUrl =
'https://open.spotify.com/playlist/37i9dQZF1DWX5ZOsG2Ogi1?si=41c6a392cf7d4a2b';

// Check if Spotify is installed
if (await canLaunch(spotifyUrl)) {
// Launch the url which will open Spotify
launch(spotifyUrl);
}
},
child: Text('Open Spotify'),
),
),
);
}
}

关于Flutter 使用包打开深层链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69875612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com