A word on Intents

396 views
Skip to first unread message

Gerard Hernandez

unread,
May 9, 2016, 2:11:13 PM5/9/16
to DroidScript
Hi everyone.

Android have a very powerful system to extend your application features named Intents (Intent, Pending Intents, Intent Filter, Activities for the ones that do know the inner workings of Android).

With this Intent system you can delegate specific task to other applications in order to keep simple and concise to your specific needs, this way your app:
  • Doesnt need to have persmission to access a hardware feature like camera, gps, read contacts etc, long etc.
  • Doesnt need to reinvent the wheel creating features that others apps do well, and in most of the times, have gone trough more testing than yours.
  • Users after some time get comfortable with the idea of reusing some apps for other apps. This way , if you present them the default camera all time, they will have more skill using that, than learning a need app interaction pattern, even when your approach is the best thing and a total revolution in UI metaphors. humans are beings of habits, so breaking the habit takes time, and usually is because the user want to, not because you want them to do it.
Lets take for example Whatsapp, did they created a brand new camera, no, there are intents taht allow you to launch the default camera and the path were the new video or image should be stored, it can even return you a smal bitmap of the captured image. What camera it will use, any that is available.

This bring another topic, Permissions and Features from the manifest file. The most important difference is that "PERMISSION" mean that the app will use a feature that is protected, a "USE FEATURE" means that the app requires that the hardware actually have the feature available. 
Permissions can be revoked, and features can be optional.

Returning to the intent system, I found it very useful when youre creating apps with media interaction, and the creation of lockers/launchers.  I haven't created a public fully pledged launcher per se, but have used the feature to create Kiosk apps where only few apps needs to be available.

There are 2 types of intents, Explicit and Implicit.
  • Explicit intents needs the package/activity that will be called.
  • Implicit do not need package/activity names, but need the action and the category.

Some intents need the data (uri), type (myme type) and extras, not all of them have a meaningful result so its usage depend on case to case basis.

Whenever you work with intents, keep this in mind, if you will use a implicit intent, please check if the package is installed, otherwise your app will crash.
And if you will use implicit intents, the system may rise the Resolve dialog, where you need to define what app will complete the action, until a default is selected for next times.

Custom Android ROMS usually lack of google apps, including Remix OS, and this may be a problem even when you install the play services apk. If you call an implicit intent for a calculator app and there is none, you may get into errors. this i up to the end user and I advise to inform your users of what defaults your capp is capable of calling them, so they will be the ones who need to install the apps in order to make your app fully functional.  You can call the market app with a predefined "feature" app that can handle the intent, but the user is the one who decides to install it.

Stock android ROMs do not lack of these default intents apps, but from time to time, their ROMS is a mess and problems may arise.

I have created some common intents entry in the doku wiki at http://wiki.droidscript.me.uk/  please take a look, my user there is mxgh2000.

Keep in mind that currently Onresult only returns the result code of the intent, not the data. Regards.

Manuel Lopes

unread,
May 9, 2016, 6:37:32 PM5/9/16
to DroidScript
if i understand correct this is danger because not need permissions?

Gerard Hernandez

unread,
May 9, 2016, 10:53:07 PM5/9/16
to DroidScript
Totally the opposite, your app won't do anything but ask any other app to complete the action, that app is supposed to be trusted by the user and/or the device manufacturer, so no risk of unknown side operations. You want to take a pic, send a intent, open the camera, and the user will take the image, with intents usually the user takes the last word. Way too different than you accessing the api's yourself. Intents may seem cumbersome at the beginning, but is one of the most useful features of the Android api.

salvatore fusto

unread,
May 10, 2016, 4:07:02 AM5/10/16
to DroidScript
Gerard,
can intents be usefull to baccess to contact list?
regards

Gerard Hernandez

unread,
May 10, 2016, 12:00:40 PM5/10/16
to DroidScript
yes, you can have a basic contacts access with intents. The "FULL" access is only possible trough the access of the contacts content provider, which is yet not available.

But with intents you can:
Open contacts app
Select/Pick a contact (this returns the data object mentioned below) 
View a contact ( this may need the contact id retrieved when picking it to compose the URI)  *
Create a contact
Edit a contact( this may need the contact id retrieved when picking it to compose the URI)  *


If the result option is enriched with the data send back from the intent, the we will have even more power. and youll be able to use all the options I mentioned with * mark

In android (java) when you send an intent for result, you  have 3 pieces of data back
  • The id of the intent,nothing more a code to reuse the same method for several intents, so you can use a switch/case. The way DS works makes you dont need such code,  yet.
  • The code result of the intent, this is an integer and the meaning depends on the intent.
  • The data object, this varies among intents. This is the key point.
Im posting some intents in the community wiki in the Intents section. Please take a look a i will be adding more as I test them,

Gerard Hernandez

unread,
May 10, 2016, 12:52:15 PM5/10/16
to DroidScript
Please keep in mind that most of the intents are that, Intents of actions, and the  the user is the one who haves the last word.
As all operations in data applications, there are 4 types of operation, Create, Read, Update and Delete, and the extra one Execute. Android is no different in that aspect.

There are intents that can incur in costs for the user.
Make a direct call (requires CALL_PHONE permission)
Open a URL (data usage after all, if your app do not directly access the web, but contains links as a primary way of interaction, you should inform you users of it)

There are other more quite intents that do not cause harm like:
  • Opening the dialer app with the number on it.
  • Lanching a prepopulated sms or email composer app.
  • Launching a prepopulated create new contact capture.
  • Launching maps api with a specific lat/long/zoom
  • Open contacts app
  • Open music app 
  • etc.

If you want to create a contact, no harm is made if it is inserted into the contact provider, as it will use a proven app for that, it need user confirmation, if its duplicated it will tell you and wont do it in a single shot, the same is you didn't specified the account.
The same when editing, you wont do it right away, the user will complete the action. The good thing is that you dont need  to implement the huge overhead of the contact capture UI, the accounts stuff and the contact provider.

Intents are used also to open the apps, in the launcher sample by DS team, you use one of the most common intents to open the app, There is even a intent to open a whatsapp conversation ( but it requires you to know the number before hand, again contacts pick with content URI in the data).

Intents allow you to not create parts of other applications if you dont really need it.

Gerard Hernandez

unread,
May 10, 2016, 1:16:57 PM5/10/16
to DroidScript
There are alternate ways to open an app using app.LoadURL(); like

app.LoadURL("whatsapp://send?text=Hello%20World!");

This will open the whatsapp, if installed, and will request you to pick the contact. 

This approach uses the intent underlying system, but instead of using actions,categories, packages and class names, It relies on the action.
android.action.intent.VIEW and what will do the trick is the type and the data.  

If you look into other apps manifests, (whatsapp among them) Youll find that there have intent filters with schemes like <data android:scheme="sms" />, that is the part at the beginning,( maps, whatsapp, https, here http is not a protocol per se, but the URI scheme) and types(mime types actually) like this
<data android:mimeType="image/*"/><data android:mimeType="text/plain"/>
The same scheme approach can be applied to Droidscript, it already hast the scheme "content" with some mime types, That is the reason that when you first installed Droidscript, and tried to open a web page, launch google now, or share a file, among the options was droidscript.

This method is discouraged by the W3C because it kind of breaks the way the internet works, supposedly, those schemes are not a protocol and not global, and not all the world has whatsapp, the same for smsto, mailto, tel. I personally think that it depend on the usage, is a web page, fine, use http/https, but inside your tiny world-verse, where you for sure have a lot of apps, and those schemes makes total sense, then https represent a secure webpage, http a non-secure webpage, and smsto means a number and text that I want to send. hehehe

Manuel Lopes

unread,
May 10, 2016, 5:33:41 PM5/10/16
to DroidScript
thanks for answer

salvatore fusto

unread,
May 11, 2016, 3:34:41 AM5/11/16
to DroidScript
Gerards,
very clear explanation, thanks. in your experiments, please write abiut contact access via intent if you can/want.
i suspect that it is not possible to have a list of all contacts, as in the phone contact list: am i wrong?
regards

Gerard Hernandez

unread,
May 11, 2016, 10:40:33 AM5/11/16
to DroidScript
Regarding the contacts, you can either open the default contacts app, in that case you are just launching it. There is another to pick a contact, that will make the Intent in the OnResult method (in the java internal code, currently not returned to Us) to contain the URI "content://contacts etc etc" inside of it. 

At the end depends on what you want to do, If you want to reuse an existing app if your app is not a contacts management app Or if you want to do such thing.

If is the first, I would vote in "Tell Us what you want" to have the Data returned in the intent result exposed, a json string would be great, or a Json object.

If you really want FULL access to contacts, a java plugin can be made, but that will only provide the methods to do CRUD operatios, no reusable GUI / Business Rules whatsosever.

Dave Smart

unread,
May 12, 2016, 10:05:05 AM5/12/16
to DroidScript
Hi Guys,

I'm extending the intents result functionality right now to allow a JSON return of the data.  I'm also allowing generic access to the content resolver which should allow you to get contact details and many other things too :)

Regards
David

Gerard Hernandez

unread,
May 12, 2016, 10:15:28 AM5/12/16
to DroidScript
Hi Dave.

That is support!!!! That will allow us to do more freak stuff more easily.  
Thanks a lot.

Dave Smart

unread,
May 12, 2016, 3:16:38 PM5/12/16
to DroidScript
New alpha now released :)

Manuel Lopes

unread,
May 12, 2016, 5:54:08 PM5/12/16
to DroidScript
thanks dave you are the best :)

Netpower8

unread,
Aug 7, 2016, 3:06:29 PM8/7/16
to DroidScript
On follow up to the this subject. How can you process an error if the intent failed? I have made an intent call to play store. If playstore is installed it will call it. But if playstore is disabled or not installed i should be able to get an error msg so i can open a playstore url instead of an intent.

I tried the send intent callback. It appears not to call the callback on send intent failure

Steve Garman

unread,
Aug 7, 2016, 3:31:07 PM8/7/16
to DroidScript
Why wait for the intent to fail?

if(app.IsAppInstalled( packageName ))
app.SendIntent( packageName, className, action,null,uri );
else
{
// Do your alternative action
}

Netpower8

unread,
Aug 7, 2016, 4:24:02 PM8/7/16
to DroidScript
thanks steve... the intent on playstore on the droidscript wiki made a call to app.OpenUrl()... i use a send intent instead. and it works. then i disabled playstore...

to test. the android system did not give me any response if the send intent was a success or fail. will try your suggestion. but i guess it might not work. harhar

because you made a check if the app is installed. but i disabled playstore so i dont think that call to app.IsAppInstalled will know if the app is disabled or enabled.

will try it out...

Netpower8

unread,
Aug 7, 2016, 4:31:25 PM8/7/16
to DroidScript
just tried it... just now... it still made the call to send intent even though the playstore was disabled.... but for the moment this method will do.

it just simple check if the playstore app is installed or not...

is there a way to know if the send intent was successful or not ?

Alex F

unread,
Aug 7, 2016, 4:39:02 PM8/7/16
to DroidScript
Thanks a lot Gerard!
Could someone please send the alpha and beta list link again :)
Thanks

Netpower8

unread,
Aug 7, 2016, 9:21:14 PM8/7/16
to DroidScript
@alex
Gerard posted about intents in the wiki. There is a section there. You can check it out. Rather interesting...
Reply all
Reply to author
Forward
0 new messages