您的包用于打开应用程序而不是打开深层链接。深度链接基本上只是打开一个 URL,如果可用,它将将您重定向到其关联的应用程序。
因此,要打开深层链接,您应该使用包 url_launcher 并使用它启动您的 Spotify URL。
这是一个关于 Android 的教程:
第 1 步:向您的 AndroidManifest 添加权限
您需要添加权限QUERY_ALL_PACKAGES,以便您可以使用canLaunch 验证手机上是否可以使用Spotify 应用程序。
-
android/app/src/debug/AndroidManifest.xml(如果您也希望它在调试模式下工作)
<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步:使用canLaunch和launch
这是一个基本的代码示例:
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'),
),
),
);
}
}