Tasker
Home
Direct Purchase
Download
TaskerNet
Home Automation
Usage Examples
Pre-made Projects
FAQs
Guides
Reviews
Wiki
Plugin List
Forum
Forum/Support
Legacy Forum
Userguide (5.0+)
Index: en es fr zh
1 Page: en
More
Join
AutoApps
Developers
History
Privacy Policy
Release Notes
Next Version
Feature Requests
Report Issues
Patreon

Invoking Tasks Programatically


Note: it's recommended that the plugin interface is used instead of direct access wherever possible.

Tasker's tasks and (most) actions are all easily available to external applications via broadcast of intents. Your app requires the Manifest permission net.dinglisch.android.tasker.PERMISSION_RUN_TASKS in order to use this feature.

Convenience classes called TaskerIntent and ActionCodes are available to make things easier, you just need to include them in your project source and change the package name at the top of the file.

The preference Misc / Allow External Access must be set in Tasker's UI.

From Tasker v1.0.18, you can present a task selection dialog with:

	startActivityForResult( TaskerIntent.getTaskSelectIntent(), requestCode );

Get the selected task name in your onActivityResult() with getDataString().

From Tasker v1.2.2, you can get a list of existing task names.

Following are some usage examples. If you have any trouble with these or others, please don't hesitate to ask in the forum.

Call A Pre-Defined Task

This is the simplest usage, a task already defined by the user in the Tasker UI, with no error-checking. The call is asynchronous, your app continues immediately after the broadcast.
	if ( TaskerIntent.testStatus( this ).equals( TaskerIntent.Status.OK ) ) {
		TaskerIntent i = new TaskerIntent( "MY_USER_TASK_NAME" );
		sendBroadcast( i );
	}
From Tasker v1.1.1 & TaskerIntent v1.1, you can pass as many parameters as you like to the task:
	i.addParameter( "value" );
The parameters are visible to the task when it runs as local variables %par1, %par2 etc. In the example, %par1 is set to value.

From Tasker & TaskerIntent v1.2 you can stipulate arbitrary local (all lower-case) variable names and values to make available to the task:

	i.addVariable( "%name", "value" );

Zip A File

This one performs a task which is constructed within the TaskerIntent object. The priority is set to high, so that it is likely to override any other tasks which are currently running e.g. a widget press task.
	if ( TaskerIntent.testStatus( this ).equals( TaskerIntent.Status.OK ) ) {
		TaskerIntent i = new TaskerIntent( "MY_ZIP_TASK" );

		i.addAction( ActionCodes.ZIP_FILE ).		// name of action
			addArg( "/sdcard/test.txt" ).	// argument list
			addArg( true ).			// delete original after
			addArg( 9 ).			// compression level
			setTaskPriority( 9 );		// high priority

		sendBroadcast( i );
	}

You can find a list of the available actions are their arguments in the Tasker UI. Names are case sensitive and English, even if a localized version of Tasker is installed.

You might also want to use setTaskPriority() when broadcasting multiple tasks at one, since tasks with the same priority are not guaranteed to execute in the reception order. Alternatively, you could put all the actions in a single task, since those are guaranteed to execute in order.

See the userguide for information on Task scheduling in general.

Encrypt A File And Say Something

Here's a 'real' example. This one encrypts a file and makes an announcement when it's finished.
	Status status = TaskerIntent.testStatus( this );

	switch ( status ) {
	case OK:
		TaskerIntent i = new TaskerIntent( "NEW_ENCRYPT_TASK" );
	
		// Add actions

		i.addAction( ActionCodes.ENCRYPT_FILE ).		// action code
			addArg( "/sdcard/test.txt" ).			// argument list
			addArg( TaskerIntent.DEFAULT_ENCRYPTION_KEY ).	// encryption key name
			addArg( false );				// leave key after

		i.addAction( ActionCodes.SAY ).				// action code
			addArg( "Finished!" ).				// speech text
			addArg( "com.google.android.tts:eng-gbr" ).	// speech engine
			addArg( 0 ).					// sound channel
			addArg( 5 ).					// pitch
			addArg( 5 ).					// speed
			addArg( false );				// continue task immediately

		// Setup a receiver to get a) when task finished b) success/failure
		// Note: you need to setup a new receiver for each task, because each
		// receiver is tuned to a particular broadcast task
	 
		BroadcastReceiver br = new BroadcastReceiver() {
			public void onReceive( Context context, Intent recIntent ) {
				if ( recIntent.getBooleanExtra( TaskerIntent.EXTRA_SUCCESS_FLAG, false ) )
					;  	// success, do something
				else
					Log.w( TAG, "Tasker reports failure." );

				unregisterReceiver( this );
		};

		// You probably want to unregister this if the user leaves your app e.g. in onPause
		// You may want to set a timeout in case e.g. Tasker's queue is full

		registerReceiver( br, i.getCompletionFilter() );

		// Start the task. This call exits immediately.

		sendBroadcast( i );
	default:
		Log.w( TAG, "couldn't run Tasker task: " + status );
	}

Error status values:

  • NotInstalled: no Tasker package could be found on the device
  • NoPermission: calling app does not have the needed Android permission (see above)
  • NotEnabled: Tasker is disabled by the user.
  • AccessBlocked: external access is blocked in the user preferences. You can show the user the relevant preference with e.g. startActivity( TaskerIntent.getExternalAccessPrefsIntent() )
  • NoReceiver: nothing is listening for TaskerIntents. Probably a Tasker bug.

PendingIntents

Starting with Tasker 1.0.19, TaskerIntent sets a (long) random ID as the data portion of the underlying Intent, so if you are using multiple TaskerIntents with PendingIntents you shouldn't need to worry about clashes.

Miscellaneous Functions

static boolean havePermission( Context c );
Returns true if you have acquired the necessary Manifest permission PERMISSION_RUN_TASKS. If it returns false, it may be because your app was installed before Tasker.
static boolean taskerInstalled( Context c );
Returns true if Tasker is installed on the device.
static Intent getTaskerInstallIntent( boolean marketFlag );

Gets an Intent that you can provide to startActivity() to install Tasker from Android market or a download page.