Send Intents via Java, unlimited extras, full control (no root needed)

1,089 views
Skip to first unread message

Ruslan Alcotana

unread,
Mar 22, 2015, 2:25:18 PM3/22/15
to tas...@googlegroups.com
Hello, guys!

Here is the guide how to send android intents without any limitations using Tasker. It will help you to send more than 2 extras, for example, or to specify/force type of extra (long instead of integer, string instead of float). How about setting flags manually? Maybe you just want to copy an URI from Intent intercept and parse it to create intent. There is almost nothing about this way of sending intents on the internet, so this post should be useful.

Here are the example steps to create your own intent (add a new contact):
Create an empty task. Open screenshots of these steps: Screenshots Gallery
1. Action -> Code -> Java Function
    Class or Object: Spyglass -> filter "intent" -> Choose Class - Intent (android.content)
    Function: Spyglass -> new {intent} ()
    Return {Intent} -> "myIntent" (you are not allowed to use uppercased first letter for Java objects)
2. Action -> Code -> Java Function
    Class or Object: Coffee cup -> myIntent
    Function: Spyglass -> filter "action" ->  setAction
    Return {Intent} -> nothing
    Param(String): android.intent.action.INSERT
3. Action -> Code -> Java Function
    Class or Object: Coffee cup -> myIntent
    Function: Spyglass -> filter "category" ->  addCategory
    Return {Intent} -> nothing
    Param(String): android.intent.category.DEFAULT
4. Action -> Code -> Java Function
    Class or Object: Coffee cup -> myIntent
    Function: Spyglass -> filter "category" ->  addCategory
    Return {Intent} -> nothing
    Param(String): android.intent.category.DEFAULT
5. Action -> Code -> Java Function
    Class or Object: Coffee cup -> myIntent
    Function: Spyglass -> filter "type" ->  setType
    Return {Intent} -> nothing
    Param(String): vnd.android.cursor.dir/contact (/raw_contact if you want to choose an app)
6. Action -> Code -> Java Function
    Class or Object: Coffee cup -> myIntent
    Function: Spyglass -> filter "extra" -> putExtra {Intent} (String, String)
    (pay attention to (String, String) params)
    Return {Intent} -> nothing
    Param(String): phone
    Param(String): +1234325346
7. Copy previous task to add exrtas like: name, company, email, notes (for more info google ContactsContract.Intents.Insert)
8. You need some flags for this to work (pay attention to Param(int) field)
    Action -> Code -> Java Function
    Class or Object: Coffee cup -> myIntent
    Function: Spyglass -> filter "flag" -> addFlags {Intent} (int)
    Return {Intent} -> nothing
    Param(Int): myIntent.FLAG_ACTIVITY_FORWARD_RESULT
9. You need some flags for this to work (pay attention to Param(int) field)
    Action -> Code -> Java Function
    Class or Object: Coffee cup -> myIntent
    Function: Spyglass -> filter "flag" -> addFlags {Intent} (int)
    Return {Intent} -> nothing
    Param(Int): myIntent.FLAG_ACTIVITY_NEW_TASK
10. Start your intent
    Action -> Code -> Java Function
    Class or Object: Coffee cup -> CONTEXT
    Function: Spyglass -> filter "activity" -> startActivity {} (Intent)
    Param(Intent) -> myIntent

Here is the short URI for this intent:
intent:#Intent;action=android.intent.action.INSERT;category=android.intent.category.DEFAULT;type=vnd.android.cursor.dir/raw_contact;launchFlags=0x13000000;S.phone=%2B1234325346;S.name=Mike;end

You can create an Intent from this URI using parseURI function:

1. Action -> Code -> Java Function
    Class or Object: Spyglass -> filter "intent" -> Choose Class - Intent (android.content)
    Function: Spyglass -> parseURI {Intent} (String, int)

    Return {Intent} -> myIntent
    Param(String): intent:#Intent;action=android.intent.action.INSERT;category=android.intent.category.DEFAULT;type=vnd.android.cursor.dir/raw_contact;launchFlags=0x13000000;S.phone=%2B1234325346;S.name=Mike;end
    Param(int): 318767104 (Decimal value of hexadecimal 13000000 above)

2. Start your intent
    Action -> Code -> Java Function
    Class or Object: Coffee cup -> CONTEXT
    Function: Spyglass -> filter "activity" -> startActivity {} (Intent)
    Param(Intent) -> myIntent

Try the task. Isn't it cool? Imagine "infinite" possibilities of Java programming in Tasker. M-mmmm...
Task files are attached. You can download and long-press Task tab to import files to Tasker.
Intent.tsk.xml
IntentURI.tsk.xml

Odysseos

unread,
Mar 23, 2015, 5:17:55 AM3/23/15
to tas...@googlegroups.com
Hi Rusland great tutorial, thanks for sharing. I've tried it to set an Alarm. My task returns all "green dots" but Alarm isn't set and I get a popup warn saying " Clock app has been stopped". Below the description of the task. Can you help me to understand where I'm wrong?
Thanks in advance.

Intent Set Alarm (400)
A1: Java Function [ Return:myIntent Class Or Object:Intent Function:new
{Intent} () Param: Param: Param: Param: Param: ]
A2: Java Function [ Return: Class Or Object:myIntent Function:setAction
{Intent} (String) Param:android.intent.action.SET_ALARM Param: Param: Param: Param: ]
A3: Java Function [ Return: Class Or Object:myIntent Function:addCategory
{Intent} (String) Param:android.intent.category.DEFAULT Param: Param: Param: Param: ]
A4: Java Function [ Return: Class Or Object:myIntent Function:putExtra
{Intent} (String, int) Param:android.intent.extra.alarm.HOUR Param:11 Param: Param: Param: ]
A5: Java Function [ Return: Class Or Object:myIntent Function:putExtra
{Intent} (String, int) Param:android.intent.extra.alarm.MINUTES Param:30 Param: Param: Param: ]
A6: Java Function [ Return: Class Or Object:myIntent Function:putExtra
{Intent} (String, String) Param:android.intent.extra.alarm.DAYS Param:MONDAY Param: Param: Param: ]
A7: Java Function [ Return: Class Or Object:myIntent Function:putExtra
{Intent} (String, String) Param:android.intent.extra.alarm.MESSAGE Param:Message text here. Param: Param: Param: ]
A8: Java Function [ Return: Class Or Object:myIntent Function:putExtra
{Intent} (String, boolean) Param:android.intent.extra.alarm.SKIP_UI Param:true Param: Param: Param: ]
A9: Java Function [ Return: Class Or Object:myIntent Function:addFlags
{Intent} (int) Param:myIntent.FLAG_ACTIVITY_FORWARD_RESULT Param: Param: Param: Param: ]
A10: Java Function [ Return: Class Or Object:myIntent Function:addFlags
{Intent} (int) Param:myIntent.FLAG_ACTIVITY_NEW_TASK Param: Param: Param: Param: ]
A11: Java Function [ Return: Class Or Object:CONTEXT Function:startActivity
{} (Intent) Param:myIntent Param: Param: Param: Param: ]

Ruslan Alcotana

unread,
Mar 23, 2015, 12:37:27 PM3/23/15
to tas...@googlegroups.com
android.intent.extra.alarm.DAYS:
This field is only available on Android 4.4+

The value is an ArrayList. Each item can be:

public static final int MONDAY

Value of the DAY_OF_WEEK field indicating Monday.

Constant Value: 2 (0x00000002)

I guess you need putIntegerArrayListExtra function instead of putExtra, a couple of functions to create ArrayList object (Screenshots)

I have Android version 4.2.2, so it creates the alarm only for current day. Download it and try. You can find the string version of this intent in VARS -> %URI

#Intent;action=android.intent.action.SET_ALARM;launchFlags=0x12000000;i.android.intent.extra.alarm.HOUR=9;i.android.intent.extra.alarm.MINUTES=11;S.android.intent.extra.alarm.MESSAGE=Get%20up!;end



Set_alarm.tsk.xml

Odysseos

unread,
Mar 23, 2015, 3:21:40 PM3/23/15
to tas...@googlegroups.com

Hi Ruslan, you are fantastic, now all works great!
Thanks again for your kindness.

David Wang

unread,
Mar 24, 2015, 1:08:34 AM3/24/15
to tas...@googlegroups.com
Thanks for sharing!

"Full control" ? Maybe not.
Here I have some key points to add for "Send Intents" via Java in Tasker.

1."Service Thread"(from UserGuide of latest beta version):

Java code is executed with a non-UI thread by a service.
Some implications are:
  • things which require an activity will not work e.g. showing a dialog
  • sending intents will in some cases require the flag Intent.FLAG_FROM_BACKGROUND and possibly also Intent.FLAG_ACTIVITY_NEW_TASK
Here is a post that you can refer to.


2."Static Fields"(from UserGuide of latest beta version):
Static fields (e.g. ContentResolver.EXTRA_SIZE) are not currently supported by Tasker.
A workaround is to use reflection to get (or set) the value:
res = CONTEXT.getContentResolver();
cls = res.getClass();
fld = cls.getField( EXTRA_SIZE );
%val = fld.get( null );

So these static fields in your example may not correct, e.g. "FLAG_ACTIVITY_FORWARD_RESULT", "FLAG_ACTIVITY_NEW_TASK".
8. You need some flags for this to work (pay attention to Param(int) field)
    Action -> Code -> Java Function
    Class or Object: Coffee cup -> myIntent
    Function: Spyglass -> filter "flag" -> addFlags {Intent} (int)
    Return {Intent} -> nothing
    Param(Int): myIntent.FLAG_ACTIVITY_FORWARD_RESULT
9. You need some flags for this to work (pay attention to Param(int) field)
    Action -> Code -> Java Function
    Class or Object: Coffee cup -> myIntent
    Function: Spyglass -> filter "flag" -> addFlags {Intent} (int)
    Return {Intent} -> nothing
    Param(Int): myIntent.FLAG_ACTIVITY_NEW_TASK

As for accessing static fields, see here for reference. 

joaomgcd

unread,
Mar 24, 2015, 5:53:51 AM3/24/15
to tas...@googlegroups.com
Or you could simply create the intent here then use it with AutoShare :)

Great info nonetheless!

Ruslan Alcotana

unread,
Mar 24, 2015, 8:05:13 AM3/24/15
to tas...@googlegroups.com
вторник, 24 марта 2015 г., 8:08:34 UTC+3 пользователь David Wang написал:
So these static fields in your example may not correct, e.g. "FLAG_ACTIVITY_FORWARD_RESULT",

Thanks for the key points, however the fields ARE correct in my example and work. If your constant or static field is not working in your case, you can always use its raw value instead (just google it).

public static final int FLAG_ACTIVITY_FORWARD_RESULT = Constant Value: 33554432 (0x02000000)

William Hedrick

unread,
Oct 24, 2015, 1:54:40 AM10/24/15
to Tasker
Hi Ruslan!

I think this will be very useful!

I found the URI for the intent I want to use with the Intercept Intent app. I replaced the URI in your example, but now it throws an error. The URI I want to use is below. Any ideas?

intent:#Intent;action=android.intent.action.SENDTO;type=vnd.google.android.hangouts/vnd.google.android.hangout_privileged;package=uk.co.ashtonbrsc.android.intentintercept;S.participant_gaia=##############;S.participant_name=First%20Last;S.android.intent.extra.TEXT=Test;S.account_name=first.last%40gmail.com;end

############## = a long number (recipient's unique ID?)
First%20Last = name of recipient
first.last%40gmail.com = sender's email


Thanks for writing this awesome guide!

Eric Howell

unread,
Feb 7, 2016, 7:03:50 PM2/7/16
to Tasker
This is exactly the code I was looking for.  Sometimes, AutoShare and Send Intent just do not want to get the job done.  This fixes a slew of problems..  

Cheers.

-E

Febri Iswanto

unread,
Jun 2, 2016, 3:38:20 PM6/2/16
to Tasker
Is it possible to use the same intent concept to retrieve/pick phone contact data?
Reply all
Reply to author
Forward
0 new messages