【问题标题】:Can any one tell me how to open another app using flutter?谁能告诉我如何使用颤振打开另一个应用程序?
【发布时间】:2019-04-20 07:16:58
【问题描述】:

我想使用我在 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];
    },
  );
}

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

【问题讨论】:

标签: dart flutter explicit-intent


【解决方案1】:

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

  device_apps:
  android_intent:
  url_launcher:

并将这些添加到顶部 -

import 'package:device_apps/device_apps.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:android_intent/android_intent.dart';

这里是示例代码 -

_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';
}
}

【讨论】:

  • 可能比你的答案更新,但DeviceApps 有一个DeviceApps.openApp,所以不需要使用AndroidIntent
  • DeviceApps 包不适用于 ios,还有其他适用于 ios 的解决方案吗?
  • 不支持iOS。
  • device_apps 不支持 iOS,因为它更复杂。你不能只是在 iOS 上启动任意应用程序。要打开的应用程序必须注册一个 URL Scheme,或者支持您尝试从自己的应用程序打开的通用链接。
【解决方案2】:

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

external_app_launcherFlutter 插件帮助你从你的应用中打开另一个应用

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

    dependencies:
       external_app_launcher: ^0.0.1 // add letest version
    
  2. 在你的 Dart 代码中导入,你可以使用:

    import 'package:external_app_launcher/external_app_launcher.dart';
    
  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>
    

    但就像在 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,
                  ),
                ))),
          ),
        ),
      ),
    );
  }
}

【讨论】:

  • iosUrlScheme: 'pulsesecure://', - 它有效,但如果我传递诸如 iosUrlScheme: 'pulsesecure://abc.com?arg=123' 之类的参数。这没用。你知道为什么吗? @Paresh Mangukiya
  • 未在 IOS 中测试是否也可以在 IOS 中使用?
  • 这可以打开一个具有特定网址的应用程序吗?例如,如果我有一个 youtube 链接,它会使用该链接打开 youtube 应用吗?
  • 我需要用token参数打开应用。可能吗? @MrSpecial
  • @MrSpecial 你是在真实设备还是模拟器上测试过这个?该应用程序是否必须从应用商店安装或通过 xcode 安装的调试应用程序才可以?
【解决方案3】:

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

设备应用程序:^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);
}

所以上面的代码检查你是否已经安装了应用程序。如果你已经完成了它,它将午餐应用程序,如果没有,它将打开 google playstore,这样你就可以在那里看到它。它仅适用于安卓设备。

【讨论】:

  • 您的回答非常有效,但是,您可以只使用一个包来启动应用程序:external_app_launcher。但是,我会选择另一种方式来做到这一点。我会得到设备类型,我会本机处理它。
  • device_apps ⚠️ iOS 不支持
  • 我在最后一句中提到了 mate
【解决方案4】:

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

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

【讨论】:

  • 谢谢,但我想打开特定的音乐应用,而不是在浏览器中打开网页。我能够使用 device_apps 启动音乐应用,但我也想在应用中搜索该链接。如果您能提供帮助,我会很高兴。
  • _openGanna () async {bool isInstalled = await DeviceApps.isAppInstalled('com.jio.media.jiobeats'); if (bool != false) DeviceApps.openApp('com.ganna.'); else Text("Cannot open app"); }
  • 您也可以使用 flutter_appavailability 启动特定应用程序,但您使用的是特定应用程序的包名而不是链接。在我的建议中,如果您的用户设备中未安装应用程序,请打开 playstore 或 AppStore 应用程序链接
  • 我想打开应用程序并搜索那个特定的链接,打开应用程序是不够的。我们可以使用颤振来做到这一点吗?
  • 搜索该特定链接是什么意思
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 2021-07-02
  • 2019-05-15
  • 1970-01-01
  • 2022-08-13
相关资源
最近更新 更多