Discover extras from android intent

If you are catching some unknown intent in your activity, there is simple way to discover which extras are available with the following codes.
Bundle extras = getIntent().getExtras();
Set<String> ks = extras.keySet();
Iterator<String> iterator = ks.iterator();
while (iterator.hasNext()) {

Phone’s last known location using LocationManager (android)

This is a fast code to get the last known location of the phone. If there is no exact gps-information it falls back to the network-based location info. This code is using LocationManager.

private double[] getGPS() {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
List providers = lm.getProviders(true);

/* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
Location loc = null;

for (int i=providers.size()-1; i>=0; i–) {
loc = lm.getLastKnownLocation(providers.get(i));
if (loc != null) break;

double[] gps = new double[2];
if (loc != null) {
gps[0] = loc.getLatitude();
gps[1] = loc.getLongitude();
return gps;

Double back press exit android app

Sometimes it is required to block accidental back press on android app. The following code snippet helps to block accidental back press, It also helps to exit the app by double back pressing. Get back press work only at second press and notify user to press again to exit use the following snippet of codes.
private static long back_pressed;
public void onBackPressed(){
if (back_pressed + 2000 > System.currentTimeMillis()){
Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = System.currentTimeMillis();


Android NDK Development with Genymotion Emulator

Genymotion / AndroVM is an android emulator built on x86 and Virtualbox. So it’s performance is way better than the Android default SDK Emulator. However in their latest update they’ve removed both ARM Translation and Google Play Apps. The removal of ARM Translation causes the “INSTALL_FAILED_CPU_ABI_INCOMPATIBLE” errors. And also force close of some native coded android apps. So you can’t do android native development (NDK) with Genymotion emulator. For this you have to install ARM Translation.

1. Download the following Zip file ARM Translation Installer v1.1
2. Open your Genymotion VM and go to the Homescreen
3. Drag & Drop the “” onto the Genymotion VM window.
4. It should say “File transfer in progress”, once it asks you to flash it click ‘OK’
5. Reboot your VM using ADB or you can simply close the VM window. and open the Genymotion VM again.

For installing Google APIs check this post.

Birthday wish in Java style – a java sound example

The Java Sound API can be used for controlling audio playback, audio capture, MIDI synthesis, and basic MIDI sequencing.
The following example shows how to use  Java MIDI synthesizer in its simplest form.

import javax.sound.midi.*;

public class Doremi {
public static void main(String[] args) {
int[] notes = new int[]{60, 62, 64, 65, 67, 69, 71, 72, 72, 71, 69, 67, 65, 64, 62, 60};
//plays Do Re Mi Fa (western) // Sa Re Ga Ma (indian)
int delay=50;
try {
Synthesizer synthesizer = MidiSystem.getSynthesizer();;
MidiChannel channel = synthesizer.getChannels()[0];

for (int note : notes) {
channel.noteOn(note, delay);
try {
} catch (InterruptedException e) {
} finally {
} catch (MidiUnavailableException e) {

Though its gives full control but it is complex to use this API.
JFugue is an open-source Java API for programming music without the complexities of MIDI.
The following is a example of playing notes of “Happy Birth Day” with JFugue

package january.tushar.birthday;

import org.jfugue.Pattern;
import org.jfugue.Player;

public class Main {
public static void main(String[] args) {
Player player = new Player();
Pattern pattern = new Pattern("Ci D C F Ew Ci D C G Fw Ci C6 A F E Dw Bb Bb A F G Fw");
System.out.println("Happy Birthday to you Tushar.\nPlaying piano…");;

The sample eclipse project can be downloaded from here Also the compiled JAR file can be downloaded and run with “java -jar Happy-Birthday-Tush.jar” command. It is the birthday wish for someone geeky in java programming style 😉 .