【问题标题】:Check if a particular app installed or not flutter检查是否安装了特定应用程序
【发布时间】:2021-07-09 11:28:31
【问题描述】:

我想在现有应用中实现特定应用是否安装的条件。

条件:

the first app is that which is I'm going to build

Second app that is required for run first app

  1. 如果已安装应用(第二个应用),则在第一个应用中运行 HomePage()
  2. 如果未安装应用程序,则显示安装应用程序的弹出窗口或警报。

第一个应用的根代码

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
    future: Future.delayed(Duration(seconds: 2)),
        builder: (context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting) {
            return MaterialApp(
              home: Splash(),
              debugShowCheckedModeBanner: false,
            );
          } else {
            return StreamBuilder(
              stream: Connectivity().onConnectivityChanged,
              builder: (context, AsyncSnapshot<ConnectivityResult> snapshot) {
                return snapshot.data == ConnectivityResult.mobile ||
                        snapshot.data == ConnectivityResult.wifi
                    ? MaterialApp(
                        title: 'mFollower',
                        theme: ThemeData(
                          primarySwatch: Colors.blue,
                        ),
                        home: Homepage(),
                        debugShowCheckedModeBanner: false,
                      )
                    : NoInternet();
              },
            );
          }

        });
  }
}

【问题讨论】:

    标签: flutter flutter-dependencies installed-applications


    【解决方案1】:

    这可以使用包device_apps 包来实现。这将返回 Android 设备中安装的应用列表。

    // All the apps installed in the device
    List<Application> apps = await DeviceApps.getInstalledApplications();
    

    因此,根据您的要求,您可以执行以下操作

    Future<bool> isAppInstalled() {
        return DeviceApps.getInstalledApplications().then((value) =>
            value.any((element) => element.packageName == 'com.example.app')); // Your app package id to check.
    }
    

    并且在build 方法内部使用Future builder 来获取结果。

    FutureBuilder<bool>(
            future: apps(),
            builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
              if (snapshot.hasData && snapshot.data) {
                // App is installed
                return Container(
                  color: Colors.green,
                );
              } else {
                // App is not installed
                return Container(color: Colors.redAccent);
              }
            })
    

    很遗憾,此插件仅支持 ANDROID

    【讨论】:

    • 感谢您的回答,但也许您不明白我要说的到底是什么。
    • 让我们用一个真实的例子来理解:我的手机上有两个应用程序,即First AppSecond AppSecond App 已经开发和发布。 First app 正在开发中。如果我的手机中安装了Second App,则打开First AppHomePage() 状态,否则重定向到另一个状态,如noInstalled State()。我已经在其中分享了@98​​7654334@ 的根代码,条件已经申请检查互联网连接
    • 好的。因此,当用户尝试打开已安装的 Second app 时,您正在尝试打开 First app(如果它已安装在设备中)。用户->打开第二个应用程序->如果安装了第一个应用程序打开它或者说一些错误?
    • 是的。如何在上面的现有代码中实现
    • 需要在第二个app中实现上述逻辑。
    猜你喜欢
    • 2016-04-26
    • 1970-01-01
    • 2013-09-16
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    相关资源
    最近更新 更多