intent

一个简单的 Flutter 插件,用于处理 Android Intents - 用 :heart: 编写的 Android Intents 一站式解决方案。

通过 :star 来表达您的 :heart:

intent 旨在通过Android Intents来启动另一个Android Activity。此Dart API模仿了Android Intent API,因此有关如何高效使用它、何时发送何种数据的详细信息,您可能想在此处查看:这里。它对此进行了更详尽的解释。

intent 已准备就绪,可在使用

现在发布者账号已验证

解决了长期以来请求的名称冲突问题,适用于同一设备上的多个应用程序

它有什么作用?

  • intent 是您从Flutter应用程序处理不同Android Intents的一站式解决方案。
  • 它提供了一个易于使用的Dart API,可用于启动不同种类的Android Activities。
  • 您可以查看/创建文档
  • 从文档树中选择文档
  • 打开默认的Assist Activity
  • 执行Web搜索
  • 请求将某个字符串的定义发送到默认的Assist Activity
  • 打开图像以进行编辑/查看
  • 将文本/多媒体分享到另一个Activity
  • 向特定用户发送电子邮件,同时设置CCBCC
  • 一次分享多个文档
  • 同步系统内容
  • 翻译文本
  • 设置壁纸
  • 打开任何URL
  • 打开拨号器以呼叫特定号码
  • 可以从您默认的手机Activity中选择联系人
  • 可以使用默认相机Activity捕获照片

如何使用?

请确保您在pubspec.yaml中包含了intent

定义一个术语

Intent()
        ..setAction(Action.ACTION_DEFINE)
        ..putExtra(Extra.EXTRA_TEXT, "json")
        ..startActivity().catchError((e) => print(e));

显示所需应用程序的信息

请确保使用唯一的软件包ID来引用应用程序。

Intent()
        ..setAction(Action.ACTION_SHOW_APP_INFO)
        ..putExtra(Extra.EXTRA_PACKAGE_NAME, "com.whatsapp")
        ..startActivity().catchError((e) => print(e));

显示应用程序偏好设置Activity

Intent()
        ..setAction(Action.ACTION_APPLICATION_PREFERENCES)
        ..startActivity().catchError((e) => print(e));

启动应用程序错误报告Activity

Intent()
        ..setAction(Action.ACTION_APP_ERROR)
        ..startActivity().catchError((e) => print(e));

启动默认Assist Activity

Intent()
        ..setAction(Action.ACTION_ASSIST)
        ..startActivity().catchError((e) => print(e));

报告错误

Intent()
        ..setAction(Action.ACTION_BUG_REPORT)
        ..startActivity().catchError((e) => print(e));

查看URI

要启动的Activity取决于传递的URI类型。

如果传递tel URI,则会打开拨号器。

Intent()
        ..setAction(Action.ACTION_VIEW)
        ..setData(Uri(scheme: "tel", path: "123"))
        ..startActivity().catchError((e) => print(e));

如果您传递mailto URI,它将打开电子邮件应用程序。

Intent()
        ..setAction(Action.ACTION_VIEW)
        ..setData(Uri(scheme: "mailto", path: "[email protected]"))
        ..startActivity().catchError((e) => print(e));

如果传递http/https URI,则会打开浏览器。

Intent()
        ..setAction(Action.ACTION_VIEW)
        ..setData(Uri(scheme: "https", host:"google.com"))
        ..startActivity().catchError((e) => print(e));

使用默认拨号Activity拨打号码

使用tel URI设置数据将打开拨号器,号码作为输入。

Intent()
        ..setAction(Action.ACTION_DIAL)
        ..setData(Uri(scheme: 'tel', path: '121'))
        ..startActivity().catchError((e) => print(e));

但是,如果您想在不带号码的情况下打开拨号器,请确保不设置数据字段。

Intent()
        ..setAction(Action.ACTION_DIAL)
        ..startActivity().catchError((e) => print(e));

呼叫号码

您可以直接呼叫号码,但请确保您拥有必要的权限。

Intent()
        ..setAction(Action.ACTION_CALL)
        ..setData(Uri(scheme: 'tel', path: '121'))
        ..startActivity().catchError((e) => print(e));

使用ACTION_DIAL将始终是明智的选择,因为它不需要任何权限。

创建预组合的电子邮件

Intent()
        ..setPackage("com.google.android.gm")
        ..setAction(Action.ACTION_SEND);
        ..setType("message/rfc822");
        ..putExtra(Extra.EXTRA_EMAIL, ["[email protected]"]);
        ..putExtra(Extra.EXTRA_CC, ["[email protected]"]);
        ..putExtra(Extra.EXTRA_SUBJECT, "Foo bar");
        ..putExtra(Extra.EXTRA_TEXT, "Lorem ipsum");
        ..startActivity().catchError((e) => print(e));

创建文档

文档的内容类型设置为text/plain,类别为CATEGORY_OPENABLE,文件名作为extra传递,即EXTRA_TITLE

Intent()
        ..setAction(Action.ACTION_CREATE_DOCUMENT)
        ..setType("text/plain")
        ..addCategory(Category.CATEGORY_OPENABLE)
        ..putExtra(Extra.EXTRA_TITLE, "test.txt")
        ..startActivity().catchError((e) => print(e));

您也可以使用数据字段设置文件的路径。但请确保数据字段是一个有效的路径URI。

编辑文档

您可以使用适当的Activity来编辑图像/文本或任何其他数据。

Intent()
        ..setAction(Action.ACTION_EDIT)
        ..setData(Uri(scheme: 'content',
                path:
                'path-to-image'))
        ..setType('image/*')
        ..startActivity().catchError((e) => print(e));

将联系人添加到您的联系人数据库

Intent()
        ..setAction('com.android.contacts.action.SHOW_OR_CREATE_CONTACT')
        ..setData(Uri(scheme: 'tel', path: '1234567890'))
        ..startActivity().catchError((e) => print(e));

搜索某个术语

会弹出一个符合条件的候选列表,这些列表可以提供搜索Activity。

Intent()
        ..setAction(Action.ACTION_SEARCH)
        ..putExtra(Extra.EXTRA_TEXT, 'json')
        ..startActivity().catchError((e) => print(e));

分享文本/媒体

请确保您已设置了适当的路径URI以使用EXTRA_STREAM分享文档/媒体,并正确设置了类型。

下一个将分享纯文本。要分享html格式的文本,请将类型设置为text/html

Intent()
        ..setAction(Action.ACTION_SEND)
        ..setType('text/plain')
        ..putExtra(Extra.EXTRA_TEXT, 'json')
        ..startActivity().catchError((e) => print(e));

但是,如果您想创建一个选择器,即显示系统中所有符合条件的候选者,您可以将createChooser命名参数设置为true,它默认是false

发送电子邮件到特定ID,同时设置内容和CC/BCC

Intent()
        ..setAction(Action.ACTION_SENDTO)
        ..setData(Uri(scheme: 'mailto', path: '[email protected]'))
        ..putExtra(Extra.EXTRA_CC, ['[email protected]'])
        ..putExtra(Extra.EXTRA_TEXT, 'Hello World')
        ..startActivity().catchError((e) => print(e));

使用默认Assist Activity翻译文本

Intent()
        ..setAction(Action.ACTION_TRANSLATE)
        ..putExtra(Extra.EXTRA_TEXT, "I Love Computers")
        ..startActivity().catchError((e) => print(e));

从手机中选择联系人

  • 打开默认的手机Activity,让用户选择一个联系人,选定的联系人将作为Future<List<String>>startActivityForResult()返回。
        Intent()
                ..setAction(Action.ACTION_PICK)
                ..setData(Uri.parse('content://contacts'))
                ..setType("vnd.android.cursor.dir/phone_v2")
                ..startActivityForResult().then((data) => print(data),
                onError: (e) => print(e));

使用默认相机Activity捕获图像

捕获图像的路径可以在Intent().startActivityForResult().then(() { ... } )中获取,它将以List<String>的形式提供,使用该路径打开文件,即File(data[0])。现在您可以处理该图像文件了。

        Intent()
                ..setAction(Action.ACTION_IMAGE_CAPTURE)
                ..startActivityForResult().then((data) => print(data[0]), // gets you path to image captured
                onError: (e) => print(e));

image_capture

如果您不想显示默认Activity的启动动画,请设置以下标志。

Intent()..addFlag(Flag.FLAG_ACTIVITY_NO_ANIMATION);

如果您不希望此Activity显示在最近项目中,请设置以下标志。

Intent()..addFlag(Flag.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);

如果您请求某个Activity但找不到符合条件的候选者,您将收到一条错误消息,您可以使用Future.catchError((e) {},)方法来监听该消息。

PlatformException(Error, android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SYSTEM_TUTORIAL }, null)

目前Action类中提供了一个有限数量的Action,但您始终可以使用字符串常量作为Action,这将帮助您处理更多Activity。

GitHub

https://github.com/itzmeanjan/intent