Java Code

Allows you to write and execute arbitrary Java code. The code is interpreted by the BeanShell interpreter, which uses a syntax similar to older versions of Java (pre-Java 5).

AI Code Helper

Click the magnifying glass icon next to the code field to get help writing your code. You can use the built-in AI to generate the code for you directly in the editor, or you can copy the system instructions to use with your preferred external AI (like ChatGPT, Gemini, etc.).

Available Variables

Two special variables are always available for you to use in your code: context and tasker.

The context variable is an Android Context object from a Service. Because it is not from an Activity, you must add the flag FLAG_ACTIVITY_NEW_TASK when starting new activities to avoid errors.

The tasker variable is a helper object that provides methods to interact with Tasker and the system. The available methods are:

You can also access Java objects that were created and returned by other Tasker Java actions (like Java Function) within your code.

Asynchronous Operations with RxJava

The RxJava2 library is available for handling asynchronous operations like waiting for events, performing background work, or running code after a delay. The action will automatically wait for your reactive stream to complete before proceeding.

You must end your RxJava chain with a blocking operator (like blockingAwait(), blockingGet(), or blockingFirst()). This is what tells the action to wait for your asynchronous operation to finish.

Waiting for Events with Subjects (Recommended)

To wait for an event from a callback (like in a `BroadcastReceiver` or another listener), the cleanest approach is to use a Subject. You create a subject, use it within your callback implementation to signal that an event has occurred, and then block the script until that signal is received. This is much simpler than using complex operators like Completable.create().

Example: Using a Subject to wait for a signal

import io.reactivex.subjects.CompletableSubject;

/* Create a subject that will act as our signal. */
mySignal = CompletableSubject.create();

/*
 * In another part of your code (e.g., inside the 'run' method of an
 * implemented BroadcastReceiver), you would signal completion like this:
 *
 * mySignal.onComplete();
 *
 * See the `implementClass` BroadcastReceiver example for a practical use case.
*/

/* This line will block and wait until onComplete() is called. */
mySignal.blockingAwait();

tasker.log("Signal received, script can now continue.");

Simple Delays and Timers

For basic delays, using a simple timer is still appropriate.

Example: Wait for 3 seconds

import java.util.concurrent.TimeUnit;
import io.reactivex.Completable;

/* Wait for 3 seconds on a background thread. */
Completable.timer(3, TimeUnit.SECONDS).blockingAwait();

/* This code will run after the 3-second delay. */
tasker.log("3 seconds have passed.");

Example: Get a value after a delay

import java.util.concurrent.TimeUnit;
import io.reactivex.Single;
import io.reactivex.functions.Function;

/* Get the string "Hello" after a 1 second delay. */
result = Single.timer(1, TimeUnit.SECONDS)
               .map(new Function() {
                   public Object apply(Object aLong) {
                       return "Hello";
                   }
               })
               .blockingGet();

return result; /* Returns "Hello" */
Return Value

The value from your code's return statement will be stored in the output variable specified in the "Return" field. Any variables you declare inside your code are temporary and will be discarded when the action finishes.

The scope (local or global) of the returned value is determined by the capitalization of the variable name you choose:

Warning: This is a very powerful action for advanced users. The code you write is not sandboxed and can modify your device's settings or data. The BeanShell interpreter does not support modern Java features like Generics (<...>), Lambdas, or Streams.
Example

You can use the following code to set the %wifi variable to the name of currently connected Wifi Network.

Java Code:
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;

/* Get the WifiManager service. */
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

/* If WiFi manager is unavailable or WiFi is disabled, exit. */
if (wifiManager == null || !wifiManager.isWifiEnabled()) return null;

/* Get information about the currently connected WiFi network. */
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo == null) return null;

/* Get the raw SSID string. */
String ssid = wifiInfo.getSSID();
if (ssid == null) return null;

/* Remove surrounding quotes from the SSID if they exist. */
if (ssid.startsWith("\"") && ssid.endsWith("\"")) ssid = ssid.substring(1, ssid.length() - 1);

/* Return the cleaned SSID. */
return ssid;
Return Field:
%wifi