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.
[java]
Bundle extras = getIntent().getExtras();
Set<String> ks = extras.keySet();
Iterator<String> iterator = ks.iterator();
while (iterator.hasNext()) {
Log.d("KEY", iterator.next());
}
[/java]

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.

[java]
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;
}
[/java]

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.
[java]
private static long back_pressed;
@Override
public void onBackPressed(){
if (back_pressed + 2000 > System.currentTimeMillis()){
super.onBackPressed();
}
else{
Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT).show();
back_pressed = System.currentTimeMillis();
}

}
[/java]

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 “Genymotion-ARM-Translation.zip” 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.

http://bit.ly/1bFXipG

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.

[java]
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();
synthesizer.open();
MidiChannel channel = synthesizer.getChannels()[0];

for (int note : notes) {
channel.noteOn(note, delay);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
break;
} finally {
channel.noteOff(note);
}
}
} catch (MidiUnavailableException e) {
e.printStackTrace();
}
}
}
[/java]

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

[java]
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…");
player.play(pattern);
System.out.println(":-)");
System.exit(0);
}
}
[/java]

The sample eclipse project can be downloaded from here https://github.com/tushroy/HappyBirthDaySound. 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 😉 .