Flutter应用内设备应用列表获取插件get_device_apps的使用

简介

get_device_apps 是一个用于在 Flutter 应用中获取设备上已安装应用列表的插件。它支持 Android 平台,但不支持 iOS。


使用步骤

  1. 添加依赖pubspec.yaml 文件中添加以下依赖:

    dependencies:
      get_device_apps: ^2.1.0
    
  2. 导入包 在 Dart 文件中导入插件:

    import 'package:get_device_apps/get_device_apps.dart';
    

获取已安装的应用程序列表

要获取设备上所有已安装的应用程序,可以使用以下代码:

// 获取所有已安装的应用程序
List<Application> apps = await DeviceApps.getInstalledApplications();

如果只想获取具有启动意图(可直接打开)的应用程序,可以设置 onlyAppsWithLaunchIntent 参数为 true

// 获取具有启动意图的应用程序
List<Application> apps = await DeviceApps.getInstalledApplications(
  onlyAppsWithLaunchIntent: true,
  includeSystemApps: true, // 是否包含系统应用
);

注意:默认情况下,应用程序列表不是按顺序排列的,需要手动排序。


获取单个应用程序信息

可以通过应用程序的包名获取其详细信息:

// 获取特定应用的信息
Application app = await DeviceApps.getApp('com.example.app');

检查应用程序是否已安装

通过包名检查某个应用程序是否已安装:

// 检查应用程序是否已安装
bool isInstalled = await DeviceApps.isAppInstalled('com.example.app');

打开应用程序

通过包名打开某个应用程序:

// 打开应用程序
DeviceApps.openApp('com.example.app');

打开应用程序设置页面

通过包名打开某个应用程序的设置页面:

// 打开应用程序设置页面
DeviceApps.openAppSettings('com.example.app');

卸载应用程序

卸载某个应用程序需要添加权限并调用相应的方法:

  1. AndroidManifest.xml 中添加权限:

    <uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES" />
    
  2. 调用卸载方法:

    // 卸载应用程序
    DeviceApps.uninstallApp('com.example.app');
    

包含应用程序图标

在获取应用程序信息时,可以选择包含图标,并将其显示为图片:

// 获取应用程序并包含图标
Application app = await DeviceApps.getApp('com.example.app', includeIcon: true);

// 显示图标
Image.memory(app.icon);

监听应用变化

可以通过监听应用事件来实时检测应用的安装、卸载、更新等操作:

// 监听应用变化
Stream<ApplicationEvent> apps = await DeviceApps.listenToAppsChanges();

// 监听特定应用的变化
Stream<ApplicationEvent> specificApp = DeviceApps.listenToAppsChanges()
  .where((event) => event.packageName == 'com.example.app');

完整示例代码

以下是一个完整的示例代码,展示了如何使用 get_device_apps 插件来列出和管理设备上的应用程序。

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  [@override](/user/override)
  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  List<Application> _apps = [];

  Future<void> _fetchApps() async {
    _apps = await DeviceApps.getInstalledApplications(
      onlyAppsWithLaunchIntent: true,
      includeSystemApps: false,
    );
    setState(() {});
  }

  [@override](/user/override)
  void initState() {
    super.initState();
    _fetchApps();
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('设备应用列表'),
      ),
      body: ListView.builder(
        itemCount: _apps.length,
        itemBuilder: (context, index) {
          final app = _apps[index];
          return ListTile(
            title: Text(app.name),
            subtitle: Text(app.packageName),
            leading: app.icon != null ? Image.memory(app.icon) : null,
            onTap: () {
              DeviceApps.openApp(app.packageName);
            },
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _fetchApps,
        child: Icon(Icons.refresh),
      ),
    );
  }
}

更多关于Flutter应用内设备应用列表获取插件get_device_apps的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter应用内设备应用列表获取插件get_device_apps的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


在Flutter应用中,如果你想获取设备上已安装的应用程序列表,可以使用 get_device_apps 插件。这个插件允许你获取设备上已安装的应用程序信息,如应用名称、包名、图标等。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 get_device_apps 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  get_device_apps: ^2.0.0

然后运行 flutter pub get 来安装依赖。

2. 导入插件

在你的 Dart 文件中导入 get_device_apps 插件:

import 'package:get_device_apps/get_device_apps.dart';

3. 获取应用列表

你可以使用 GetDeviceApps.getInstalledApplications 方法来获取设备上已安装的应用程序列表。这个方法返回一个 Future<List<Application>>,其中 Application 是一个包含应用信息的类。

Future<void> getInstalledApps() async {
  List<Application> apps = await GetDeviceApps.getInstalledApplications(
    onlyAppsWithLaunchIntent: true, // 只获取可以启动的应用
    includeSystemApps: true, // 包括系统应用
    includeAppIcons: true, // 包括应用图标
  );

  for (var app in apps) {
    print('App Name: ${app.appName}');
    print('Package Name: ${app.packageName}');
    print('Version: ${app.versionName}');
    print('Icon: ${app.icon}');
    print('-----------------------------');
  }
}

4. 在UI中显示应用列表

你可以将获取到的应用列表显示在 Flutter 的 UI 中。例如,使用 ListView 来显示应用名称和图标:

class AppListScreen extends StatefulWidget {
  [@override](/user/override)
  _AppListScreenState createState() => _AppListScreenState();
}

class _AppListScreenState extends State<AppListScreen> {
  List<Application> apps = [];

  [@override](/user/override)
  void initState() {
    super.initState();
    getInstalledApps();
  }

  Future<void> getInstalledApps() async {
    List<Application> installedApps = await GetDeviceApps.getInstalledApplications(
      onlyAppsWithLaunchIntent: true,
      includeSystemApps: true,
      includeAppIcons: true,
    );

    setState(() {
      apps = installedApps;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Installed Apps'),
      ),
      body: ListView.builder(
        itemCount: apps.length,
        itemBuilder: (context, index) {
          Application app = apps[index];
          return ListTile(
            leading: app.icon != null ? Image.memory(app.icon) : Icon(Icons.android),
            title: Text(app.appName),
            subtitle: Text(app.packageName),
          );
        },
      ),
    );
  }
}

5. 权限

在 Android 上,获取应用列表需要 QUERY_ALL_PACKAGES 权限。你需要在 AndroidManifest.xml 文件中添加以下权限:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>

6. 注意事项

  • get_device_apps 插件在 iOS 上的支持有限,可能无法获取所有应用信息。
  • 获取应用图标可能会增加内存使用,特别是在应用列表较大的情况下。

7. 完整示例

以下是一个完整的示例,展示了如何在 Flutter 应用中使用 get_device_apps 插件获取并显示设备上已安装的应用程序列表:

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Installed Apps',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: AppListScreen(),
    );
  }
}

class AppListScreen extends StatefulWidget {
  [@override](/user/override)
  _AppListScreenState createState() => _AppListScreenState();
}

class _AppListScreenState extends State<AppListScreen> {
  List<Application> apps = [];

  [@override](/user/override)
  void initState() {
    super.initState();
    getInstalledApps();
  }

  Future<void> getInstalledApps() async {
    List<Application> installedApps = await GetDeviceApps.getInstalledApplications(
      onlyAppsWithLaunchIntent: true,
      includeSystemApps: true,
      includeAppIcons: true,
    );

    setState(() {
      apps = installedApps;
    });
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Installed Apps'),
      ),
      body: ListView.builder(
        itemCount: apps.length,
        itemBuilder: (context, index) {
          Application app = apps[index];
          return ListTile(
            leading: app.icon != null ? Image.memory(app.icon) : Icon(Icons.android),
            title: Text(app.appName),
            subtitle: Text(app.packageName),
          );
        },
      ),
    );
  }
}
回到顶部