OAuth & OSM

The number of libraries, APIs available today to perform variegated tasks is profuse. Despite the abundance, sometimes alternatives are minimal and from these scarce resources, a solution that fits our specific needs are inconsequential. Lately working on OAuth for a project based on OpenStreetMap, I could not find a convenient guide to understand and implement. Persevering on making it functional, I authorized my Android application the access to OSM servers.

By the use of Signpost, things became a little simpler. Use of Twitter API, Facebook API with signpost is all over the web. A novice developer of Android like me cannot go on without a little fretting. Further in this post, I am sharing the screenshots of the authentication and authorization.

The screenshots are self-describing. In an upcoming post, I will write a tutorial on how to execute this task.

Change Main Activity in Android

Often we miss out adding splash screen of the application in the initial development phases, or for an instance you might want to change the main activity or the launcher activity at some later stage of development. In order to incorporate this change, a minor modification in AndroidManifest.xml file is to be done.

Say you have two activities A and B. Say initially A is the main activity and now you want to make activity B as the main activity. Open the AndroidManifest.xml file. Initially the entry for activity A in the manifest looks like:

<activity android:name=".A">
    <intent -filter>
        <action android:name="android.intent.action.MAIN"></action>
        <category android:name="android.intent.category.LAUNCHER"></category>
    </intent>
</activity>

Either delete the lines in the intent-filter tag or replace the activity name with B and add a new entry for activity A.

Overall the activity B’s entry should be like:

<activity android:name=".B">
    <intent -filter>
        <action android:name="android.intent.action.MAIN"></action>
        <category android:name="android.intent.category.LAUNCHER"></category>
    </intent>
</activity>

A single line entry for activity A will look like:

<activity android:name=".A"></activity>

It’s pretty simple and clear, all you have to do is replace the name or add a new entry.

Passing Data using SharedPreferences

Sometimes while passing data between two activities, we are just concerned with passing data and not launching the activity to which the data is passed. SharedPreferences make this data passing easier without necessitating launch of the recipient activity. The usage is discussed briefly below:

Say you have two activities A and B where A is the sender and B is the recipient. Use the following code in activity A.

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(A.this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("string", "This is a string.");
editor.putInt("number", 50);
editor.commit();

In activity B, use the following code to receive the variables from sender activity:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String s = prefs.getString("string", "hello");
Integer i = prefs.getInt("number", 0);

Default values should be set in the getString(), getInt() or any of the get methods used in the recipient activity. The variables in which the received values are stored can be used for further processing in activity B.

For further details on SharedPreferences, the official documentation can be checked at this link.

Android Application Execution Error

Running my application in Android Emulator, I faced this error:

error:execution failed for task ':app:compiledebugjavawithjavac'.

My application got build up and ran in the emulator despite ignoring this issue. However, running it on my USB connected device was not a success.

Here’s how it is fixed. It demanded the environment variable JAVA_HOME to be set up. Open up your .bashrc file in your favorite editor. Add export JAVA_HOME="/path/to/your/jdk/installation" to the end of this file. I set the value to /usr/lib/jvm/java-1.8.0-openjdk-amd64. This set up the JAVA_HOME for me.

Next .gradle directory in the application directory needs to be deleted and then go to Build > Make Project in the Android Studio menu. This builds up the application’s fresh .gradle directory. And Run your application successfully.