谁能告诉我如何使用flutter打开另一个应用程序?

Lok*_*rki 10 dart explicit-intent flutter

我想使用我在 firebase 中的链接数据打开一堆音乐应用程序链接。我想打开,amazonPrimeMusic、Ganna、Spotify、Wynk、JioSavaan 等等。

Widget buildResultCard(data) {
  List items = [Text(data['Ganna']),
    IconButton(icon:Icon(Icons.all_inclusive),
      onPressed: ()=> {Text("Ganna")}
    ),

    Text(data['Wynk']),
    IconButton(icon:Icon(Icons.all_inclusive),
      onPressed: ()=> {Text("Ganna")}
    ),

    Text(data['JioSavaan']),
    IconButton(icon:Icon(Icons.all_inclusive),
      onPressed: ()=> {Text("Ganna")}
    ),

    Text(data['PrimeMusic']),
    IconButton(icon:Icon(Icons.all_inclusive),
      onPressed: ()=> {Text("Ganna")}
    )
  ];

  return ListView.builder(
    padding: EdgeInsets.only(top: 20),
    itemCount: items.length,
    itemBuilder: (BuildContext context, int index) {
      return items[index];
    },
  );
}
Run Code Online (Sandbox Code Playgroud)

当我点击列表中的按钮时,它应该打开链接所在的特定应用程序,例如 AmazonPrimeMusic 链接,它应该打开亚马逊音乐应用程序。

Lok*_*rki 11

将此添加到依赖项下的 pubspec.yaml 文件 -

  device_apps:
  android_intent:
  url_launcher:
Run Code Online (Sandbox Code Playgroud)

并将这些添加到顶部 -

import 'package:device_apps/device_apps.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:android_intent/android_intent.dart';
Run Code Online (Sandbox Code Playgroud)

这是示例代码 -

_openJioSavaan (data) async
{String dt = data['JioSavaan'] as String;
  bool isInstalled = await DeviceApps.isAppInstalled('com.jio.media.jiobeats');
if (isInstalled != false)
 {
    AndroidIntent intent = AndroidIntent(
      action: 'action_view',
      data: dt
  );
  await intent.launch();
 }
else
  {
  String url = dt;
  if (await canLaunch(url)) 
    await launch(url);
   else 
    throw 'Could not launch $url';
}
}
Run Code Online (Sandbox Code Playgroud)

  • 它不支持iOS。 (3认同)
  • 可能比你的答案新,但是“DeviceApps”有一个“DeviceApps.openApp”,所以不需要使用“AndroidIntent” (2认同)

Ary*_*yan 8

您可以使用flutter_appavailability包。这个插件允许你检查一个应用程序是否安装在移动设备上,使用这个插件你可以启动一个应用程序。

如果已经安装,则使用url_launcher在 WebView 中启动否则打开链接。

  • `_openGanna() async {bool isInstalled = await DeviceApps.isAppInstalled('com.jio.media.jiobeats'); if (bool != false) DeviceApps.openApp('com.ganna.'); else Text("无法打开应用程序"); }` (2认同)

小智 6

您好,您实际上需要两个包。使用前检查版本。首先,您需要应用程序的 id。例如,对于 facebook lite,ID 为 com.facebook.lite。如果你去 Playstore 点击共享并复制链接,你就会找到 ID。facebook lite 的链接是https://play.google.com/store/apps/details?id=com.facebook.lite从这里你可以很容易地了解到 id 是在“id=”之后。在其他应用程序上也是如此。

device_apps:^2.1.1 url_launcher:^6.0.3

try {
  ///checks if the app is installed on your mobile device
  bool isInstalled = await DeviceApps.isAppInstalled('si.modula.android.instantheartrate');
  if (isInstalled) {
     DeviceApps.openApp("si.modula.android.instantheartrate");
   } else {
     ///if the app is not installed it lunches google play store so you can install it from there
   launch("market://details?id=" +"si.modula.android.instantheartrate");
   }
} catch (e) {
    print(e);
}
Run Code Online (Sandbox Code Playgroud)

因此上面的代码检查您是否已经安装了该应用程序。如果您已完成此操作,它将启动该应用程序,如果没有,它将打开 google playstore,以便您可以在那里看到它。它仅适用于 Android 设备。


Par*_*iya 5

要在 Flutter 中实现此功能,请创建原生平台集成,或使用现有插件,例如external_app_launcher

external_app_launcher Flutter 插件可帮助您从您的应用程序中打开另一个应用程序

  1. 将此添加到您的包的 pubspec.yaml 文件中:

    dependencies:
       external_app_launcher: ^0.0.1 // add letest version
    
    Run Code Online (Sandbox Code Playgroud)
  2. 导入您的 Dart 代码,您可以使用:

    import 'package:external_app_launcher/external_app_launcher.dart';
    
    Run Code Online (Sandbox Code Playgroud)
  3. 入门

  • 用于在 android 中打开应用程序

    要在 android 中从您的应用程序打开外部应用程序,您需要提供应用程序的 packageName。

    如果插件在设备中找到该应用程序,它将被打开,但如果该应用程序未安装在设备中,则它会让用户访问该应用程序的 Playstore 链接。

    但是,如果您不想在未安装应用程序的情况下导航到 Playstore,则将该openStore属性设置为false.

  • 用于在 ios 中打开应用程序

    在 Ios 中,要从您的应用程序打开外部应用程序,您需要提供目标应用程序的 URLscheme。

    在您的部署目标大于或等于 9 时还需要更新 infoPlist 中的外部应用程序信息。

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>pulsesecure</string> // url scheme name of the app
    </array>
    
    Run Code Online (Sandbox Code Playgroud)

    但就像在 Android 中一样,如果在设备中找不到应用程序,它不会导航到 store(appStore)。

    为此,您需要提供应用程序的 iTunes 链接。

    有关入门的更多信息:https : //pub.dev/packages/external_app_launcher#getting-started

代码说明

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

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
  }

  Color containerColor = Colors.red;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Container(
            height: 50,
            width: 150,
            child: RaisedButton(
                color: Colors.blue,
                onPressed: () async {
                  await LaunchApp.openApp(
                    androidPackageName: 'net.pulsesecure.   pulsesecure',
                    iosUrlScheme: 'pulsesecure://',
                    appStoreLink:
                        'itms-apps://itunes.apple.com/us/   app/pulse-secure/id945832041',
                    // openStore: false
                  );
                  // Enter thr package name of the App you  want to open and for iOS add the     URLscheme to the Info.plist file.
                  // The second arguments decide wether the     app redirects PlayStore or AppStore.
                  // For testing purpose you can enter com. instagram.android
                },
                child: Container(
                    child: Center(
                  child: Text(
                    "Open",
                    textAlign: TextAlign.center,
                  ),
                ))),
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)