Skip to content

Using the tagin! Android library

Elyas BHY edited this page Aug 13, 2013 · 10 revisions

Android developers can also make use of the tagin! services through an Android library. Here are some minimal examples on how to integrate tagin! in your Android apps.

All requests made through the library interface are executed asynchronously, and are offloaded to their own separate thread.


MyActivity.java:

public class MyActivity extends Activity {

    // Maintain a reference to the manager
    private TaginManager mTaginManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Instantiate a new Manager. Constructor argument must be of type Context.
        mTaginManager = new TaginManager(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Register your receiver with the desired intent filters.
        registerReceiver(mReceiver, new IntentFilter(TaginService.ACTION_URN_READY));
        registerReceiver(mReceiver, new IntentFilter(TaginService.ACTION_NEIGHBOURS_READY));
    }

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }

    // Initiates a request to the server
    private void requestURN() {
        mTaginManager.apiRequest(TaginService.REQUEST_URN);
    }

    // Create a receiver that will listen to the server's response
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(TaginService.ACTION_URN_READY)) {
                String urn = intent.getStringExtra(TaginService.EXTRA_QUERY_RESULT);
                // Do some work with the received result
                // ...
            }
        }
    };
}

AndroidManifest.xml:

You need to add the following lines to your manifest, in order to be registered for listening to the requests results.

<service
    android:name="ca.idrc.tagin.lib.TaginService"
    android:exported="true" >
    <intent-filter>
        <action android:name="ca.idrc.tagin.lib.ACTION_URN_READY" />
        <action android:name="ca.idrc.tagin.lib.ACTION_NEIGHBOURS_READY" />
    </intent-filter>
</service>

Interacting with the tags serivce

Developers can also make use of the tags service in their Android activities, by implementing the GetLabelTaskListener and/or SetLabelTaskListener interfaces. An example is provided below.

MyActivity.java:

public class MyActivity extends Activity implements GetLabelTaskListener, SetLabelTaskListener {

    // Initiates a request to the server to retrieve the label of the specified URN
    private void getLabel(String urn) {
        GetLabelTask<MyActivity> task = new GetLabelTask<MyActivity>(this, urn);
        task.execute();
    }

    // Initiates a request to the server to assign a label to the specified URN
    private void setLabel(String urn, String label) {
        SetLabelTask<MyActivity> task = new SetLabelTask<MyActivity>(this, urn, label);
        task.execute();
    }

    @Override
    public void onGetLabelTaskComplete(String urn, String label) {
        // Do some work with the URN and its label passed in the parameters
    }

    @Override
    public void onSetLabelTaskComplete(Boolean isSuccessful) {
        if (isSuccessful) {
            // Update UI, do some work
        } else {
            // Server encountered an error
        }
    }
}