检查是否安装了特定的应用程序

问题描述

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

条件:

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,AsyncSnapshot<ConnectivityResult> snapshot) {
                return snapshot.data == ConnectivityResult.mobile ||
                        snapshot.data == ConnectivityResult.wifi
                    ? MaterialApp(
                        title: 'mFollower',theme: ThemeData(
                          primarySwatch: Colors.blue,),home: Homepage(),)
                    : NoInternet();
              },);
          }

        });
  }
}

解决方法

这可以使用包 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...