检查是否安装了特定的应用程序 [英] Check if a particular app installed or not flutter
本文介绍了检查是否安装了特定的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望在现有应用程序中实现特定应用程序已安装或未安装的条件。
条件:
the first app is that which is I'm going to build
Second app that is required for run first app
- 如果安装了应用程序(第二个应用程序),则在第一个应用程序中运行
HomePage()
- 如果未安装应用程序,则显示安装该应用程序的弹出窗口或警报。
第一个应用的根代码
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();
},
);
}
});
}
}
推荐答案
可以使用包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
这篇关于检查是否安装了特定的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文