Table of Contents
Understanding Intents
Intents are a fundamental concept in Android app development. They are a way for apps to communicate with each other, and allow users to initiate actions like sending a text message or opening a web page. Intents can be either explicit or implicit, and can be either broadcast or ordered.
Explicit & Implicit differences
- Explicit intents are used to start a specific app component, such as an activity or a service. They include the name of the component and any data that needs to be passed to it.
- Implicit intents, on the other hand, are used to start an app component that can handle a particular action, such as opening a web page or sending an email.
Broadcast & Ordered differences
- Broadcast intents are used to send a message to all interested parties.
- Ordered intents are used to send a message to one interested party at a time.
Sending Intents with Tasker
Actions are the most important parameter of an intent, as they specify what the intent should do. For example, the ‘android.intent.action.VIEW’ action can be used to open a web page, while the ‘android.intent.action.SEND’ action can be used to send an email or text message. Categories are used to further specify the intent’s purpose, while data and extras are used to pass additional information to the app component that handles the intent.
How to create a Task
- To create a new task in Tasker, you can tap the plus icon and select ‘Task’.
- Then, to add an ‘Intent’ base action to the task search for and select ‘Intent’ in the action list.
- Within the intent edit screen you can define the intent parameters such as action, category, data, and extras.
Handling Intents with Tasker
When Tasker receives an intent that matches the filters specified in the profile, it can trigger a task to perform an action. Examples of handling intents with Tasker include reacting to notifications, changing settings based on the time of day, and responding to voice commands.
How to create a Profile
- To create a new profile in Tasker, you can tap the plus icon and select ‘Profile’.
- Then, to handle a broadcast intent search for and select ‘Intent Received’ under ‘Event’.
- Within the intent edit screen you can define the intent filters such as action, category, data, and extras.
Examples
The following example demonstrates how to match an intent using Tasker, the intent allow you to determine if a Bluetooth device is connected.
The Tasker XML and profile description has been provided below.
Monitor Bluetooth device connected
This Profile will monitor for intents broadcasts on action: android.bluetooth.device.action.ACL_CONNECTED
when this event triggers the connected device MAC address will be in the EXTRA_DEVICE
variable which in Tasker is accessed with: %android_bluetooth_device_extra_device
.
In this example the variable: %BLUEMAC_CONNECTED
is set to the value of %android_bluetooth_device_extra_device
. You can add your own requirement here.
Download the XML for: Profile: Bluetooth Connected Monitor – Example from GitLab
Tasker Profile description
Profile: Bluetooth Connected Monitor - Example
Event: Intent Received [ Action:android.bluetooth.device.action.ACL_CONNECTED Cat:Default Cat:None Scheme:* Mime Type:* ]
Enter Task: Bluetooth Device Connect Events - Example
A1: Variable Set [
Name: %BLUEMAC_CONNECTED
To: %android_bluetooth_device_extra_device
Structure Output (JSON, etc): On ]
Android Java equivalent
This example demonstrates the same functionality in Java.
public class MainActivity extends AppCompatActivity {
private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
String deviceName = device.getName();
Log.d('Bluetooth', 'Device connected: ' + deviceName);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED);
registerReceiver(mBroadcastReceiver, intentFilter);
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(mBroadcastReceiver);
}
}
Update: The latest entry in this series explores automation options using Tasker with the Flipper Zero and expands on the examples provided here.
Some truly interesting details you have written.Aided me a lot, just what I was searching for : D.