Android Tutorial How to write a tasker plugin

Tasker is a pretty legendary app that allows you to automate everything on your phone:
https://play.google.com/store/apps/details?id=net.dinglisch.android.taskerm

Developer reference:
Reference: http://www.twofortyfouram.com/developer.html
Tasker plugins work with Locale and Tasker.


Here is how to write an 'Action' plugin for Tasker
An action plugin, is added in the Tasks. You select Action Category > Plugin > Your app.
This allows tasker to execute your app based on when certain conditions happen.

There are two main requirements:
  • You need to have an Edit Activity and its intent. This shows the settings screen in tasker, when your select "Edit" and allows to customize the actions of your app.
  • You need to have a broadcast receiver to Fire the setting. This is when your app is triggered and does its thing.

Setting up the Edit Activity
This can be any activity. It will show the settings available from your app.
To pass data back to Tasker, when your activity finishes, you need to do the following:
B4X:
Dim i As Intent
i.Initialize("","")
i.PutExtra("Data","MyData")

Activity.SetActivityResult(-1,i)
Activity.Finish
SetActivityResult is imperative, so tasker knows whether the result was positive, or cancelled.

You also need to set up the manifest for Edit Activity:
B4X:
AddActivityText(EditActivity,"<intent-filter>
                    <action android:name="com.twofortyfouram.locale.intent.action.EDIT_SETTING" />
                </intent-filter>")
SetActivityAttribute(EditActivity,android:icon, "@drawable/icon")
SetActivityAttribute(EditActivity,android:label, "$LABEL$")
SetActivityAttribute(EditActivity,android:exported, "true")


Setting up the Fire Receiver
This is the hard part.
Tasker will broadcast an intent, that you need to receive.

In the manifest, you set it up like this:
B4X:
AddApplicationText("<receiver android:name="com.maximussoft.taskerreceiver.TaskerReceiver"
          android:exported="true">
            <intent-filter>
                <action android:name="com.twofortyfouram.locale.intent.action.FIRE_SETTING" />
            </intent-filter>
        </receiver>")

Now, to receive this broadcast, we need a broadcast receiver. The code is simple for that, however a library is needed. I dont know if there is a way to have an application broadcast receiver without a library.

Here is the java code needed:
The problem is that we start our activity or service from this receiver, we have to hardcode our intent action into it.

B4X:
public final class TaskerReceiver extends BroadcastReceiver
{

    /**
    * @param context {@inheritDoc}.
    * @param intent the incoming {@link com.twofortyfouram.locale.Intent#ACTION_FIRE_SETTING} Intent. This
    *            should contain the {@link com.twofortyfouram.locale.Intent#EXTRA_BUNDLE} that was saved by
    *            {@link EditActivity} and later broadcast by Locale.
    */
    @Override
    public void onReceive(final Context context, final Intent intent)
    {
        /*
        * Always be strict on input parameters! A malicious third-party app could send a malformed Intent.
        */
        this.abortBroadcast();
        IntentWrapper in = new IntentWrapper();
        in.setObject(intent);
        BA.Log("Broadcast Received " + intent.getAction());
        Intent send = new Intent("com.maximussoft.taskerplugin.execute");
        send.putExtra("Intent", intent);
       
          context.startService(send);
         
    }
}

So now after the broadcast receiver is fired, it will start another intent in our B4A activity or service, for that we add another recevier in the manifest:

B4X:
AddActivityText(TaskerService,"<intent-filter>
                    <action android:name="com.maximussoft.taskerplugin.execute" />
                </intent-filter>")


In this way, the service is started.

If someone can help simplify this flow it will be great. :)
Otherwise, people can write their particular broadcast receivers, in notepad and use SLC.
Or if someone is interested in more information they can contact me.
 

thedesolatesoul

Expert
Licensed User
Longtime User
Still this tutorial doesnt work 100%.
There is an issue in the broadcast receiver.
If I do send back the original intent as an extra to a new intent, I am unable to get the extra again.
While the HasExtra("Intent") returns true, I cannot do
Dim in as Intent = StartingIntent.GetExtra("Intent")
I end up with an uninitialized intent :(
So the workaround for now, I hardcoded which extras I want to send back in the java code.
 

thedesolatesoul

Expert
Licensed User
Longtime User
Yes, I have tried that library but it didnt work for me.
That library instantiates a Broadcast receiver within it, therefore it requires an activity or service to be running. Also, it seems to be an activity/service level receiver rather than application level receiver.
Anyway, I did many variations of it but could not get it to work :(
 

cheveguerra

Member
Licensed User
Longtime User
I know it is an OOOOLDDD post, but is there somewhere an example project to start ?

Best regards
 
Top