android intents
a simple flutter plugin to deal with android intents – your one stop solution for android intents, written with :heart:.
show some :heart: by putting :star:
intent
tries to help you in launching another android activity using android intents. this dart api replicates android intent api, so for detailed information on how to use it efficiently, when to send what kind of data, you may be interested in taking a look here. which explains things more elaborately.
intent
is readily available for use.
now publisher account’s verified
resolves long requested name conflict issue, for multiple applications on same device
what does android intents do ?
intent
is your one stop solution for handling different android intents from flutter app.- it provides an easy to use dart api, which can be used to launch different kind of android activities
- you can view / create documents
- pick document(s) from document tree
- open default assist activity
- perform a web search
- request definition of a certain string to default assist activity
- open image for editing / viewing
- share text / multimedia to another activity
- send email to specific user, while also setting
cc
&bcc
- share multiple documents at a time
- sync system content
- translate text
- set wallpaper
- open any
url
- open dialer for calling a specific number
- can pick a contact from your default phone activity
- can capture a photo using default camera activity
how to use it ?
well make sure, you include intent
in your pubspec.yaml
.
define a term :
intent()
..setaction(action.action_define)
..putextra(extra.extra_text, "json")
..startactivity().catcherror((e) => print(e));
show desired application info :
make sure you address app using its unique package id.
intent()
..setaction(action.action_show_app_info)
..putextra(extra.extra_package_name, "com.whatsapp")
..startactivity().catcherror((e) => print(e));
show application preference activity :
intent()
..setaction(action.action_application_preferences)
..startactivity().catcherror((e) => print(e));
launch application error reporting activity :
intent()
..setaction(action.action_app_error)
..startactivity().catcherror((e) => print(e));
launch default assist activity :
intent()
..setaction(action.action_assist)
..startactivity().catcherror((e) => print(e));
report bug :
intent()
..setaction(action.action_bug_report)
..startactivity().catcherror((e) => print(e));
view a uri :
which activity to be launched, depends upon type of uri passed.
in case of passing tel
uri, opens dialer up.
intent()
..setaction(action.action_view)
..setdata(uri(scheme: "tel", path: "123"))
..startactivity().catcherror((e) => print(e));
if you pass a mailto
uri, it’ll open email app.
intent()
..setaction(action.action_view)
..setdata(uri(scheme: "mailto", path: "[email protected]"))
..startactivity().catcherror((e) => print(e));
in case of http
/ https
uri, opens browser up.
intent()
..setaction(action.action_view)
..setdata(uri(scheme: "https", host:"google.com"))
..startactivity().catcherror((e) => print(e));
dial a number using default dial activity :
setting data using tel
uri, will open dialer, number as input.
intent()
..setaction(action.action_dial)
..setdata(uri(scheme: 'tel', path: '121'))
..startactivity().catcherror((e) => print(e));
but if you’re interested in opening dialer without any numbers dialer, make sure you don’t set data field.
intent()
..setaction(action.action_dial)
..startactivity().catcherror((e) => print(e));
calling a number :
you can simply call a number, but make sure you’ve necessary permissions to do so.
intent()
..setaction(action.action_call)
..setdata(uri(scheme: 'tel', path: '121'))
..startactivity().catcherror((e) => print(e));
it’ll always be a wise decision to use action_dial
, because that won’t require any kind of permissions.
create precomposed email :
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));
create a document :
content type of document is set text/plain
, category is category_openable
and file name is passed as an extra i.e. 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));
you can also set path of file using data field. but make sure data field is a valid path uri.
edit document :
you can edit image/ text or any other kind of data using appropriate activity.
intent()
..setaction(action.action_edit)
..setdata(uri(scheme: 'content',
path:
'path-to-image'))
..settype('image/*')
..startactivity().catcherror((e) => print(e));
add a contact to your contact database :
intent()
..setaction('com.android.contacts.action.show_or_create_contact')
..setdata(uri(scheme: 'tel', path: '1234567890'))
..startactivity().catcherror((e) => print(e));
search for a term :
opens up a list of eligible candidates, which can provides search activity.
intent()
..setaction(action.action_search)
..putextra(extra.extra_text, 'json')
..startactivity().catcherror((e) => print(e));
share text/ media :
make sure you’ve set appropriate path uri for sharing documents/ media using extra_stream
and also set type properly.
following one will share a plain text. for sharing html formatted text, set type to text/html
.
intent()
..setaction(action.action_send)
..settype('text/plain')
..putextra(extra.extra_text, 'json')
..startactivity().catcherror((e) => print(e));
but if you’re interested in creating a chooser i.e. all eligible candidates shown up system, which can handle this intent, make sure you set createchooser
named parameter to true
, which is by default false
.
send email to certain id while setting content and 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));
translate a text using default assist activity :
intent()
..setaction(action.action_translate)
..putextra(extra.extra_text, "i love computers")
..startactivity().catcherror((e) => print(e));
pick a contact up from phone :
- opens default phone activity and let user pick a contact up, selected contact will be returned as
future<list<string>>
fromstartactivityforresult()
.
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));
capture image using default camera activity :
path to captured image can be grabbed from intent().startactivityforresult().then(() { ... } )
, which will be provided in form of list<string>
, open file using that path, file(data[0])
. now you can work on that image file.
intent()
..setaction(action.action_image_capture)
..startactivityforresult().then((data) => print(data[0]), // gets you path to image captured
onerror: (e) => print(e));
if you’re not interested in showing default activity launch animation, set following flag.
intent()..addflag(flag.flag_activity_no_animation);
if you don’t want this activity to be displayed in recents, set following flag.
intent()..addflag(flag.flag_activity_exclude_from_recents);
if you request for certain activity and no eligible candidate was found, you’ll receive one error message, which you can listen for using future.catcherror((e) {},)
method.
platformexception(error, android.content.activitynotfoundexception: no activity found to handle intent { act=android.intent.action.system_tutorial }, null)
currently a limited number of actions are provided in action
class, but you can always use a string constant as an action
, which will help you in dealing with many more activities.
Comments are closed.