Posts

Showing posts from June, 2010

Showing Route through Google Map in Android

Image
To show the route between two places in google map, we can use

"http://maps.google.com/maps?&saddr=13.042206,80.17000&daddr=9.580000,78.100000"

this url.

Here I used the latitude and longitude of Chennai and Madurai, two cities in India.

The complete code snippet is,



public class LaunchMap extends MapActivity {

Uri uri;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uri =Uri.parse("http://maps.google.com/maps?&saddr=13.042206,80.17000&daddr=9.580000,78.100000");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
finish();
}

@Override
protected boolean isRouteDisplayed() {
return false;
}
}



For showing this, we need to include the google map library in Manifest file.

A the time of launching , a pop up will ask to open in Browser or Google maps, just click on google map and it will show a list displaying the step by step direction as,



If we click on the list item …

Live Audio streaming using MediaPlayer in Android

Live Audio streaming in android, from 1.6 sdk onwards is become so easy. In setDataSource() API directly pass the url and audio will play without any issues.

The complete code snippet is,


public class AudioStream extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = "http://www.songblasts.com/songs/hindi/t/three-idiots/01-Aal_Izz_Well-(SongsBlasts.Com).mp3";
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(url);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare();
mp.start();
} catch (Exception e) {
Log.i("Exception", "Exception in streaming mediaplayer e = " + e);
}
}
}


The Url I used here is a song from a bollywood film 3 Idiots.


Happy Coding

Creating SDCard and using it in emulator

The command for creating sdcard is

mksdcard 16M image.iso

Here we created a sdcard of name "image" with 16MB of size.

For using the sdcard in emulator, give this command

-sdcard /image.iso

in "Additional Emulator Command Line Options"

INSTALL_PARSE_FAILED_NO_CERTIFICATES in Android

Today, when I am doing a project in Android, suddenly I got this error. Actually, when I got this error is, after putting a png file in drawable called "close_btn_nmb". After putting this png file and try to run, I got tINSTALL_PARSE_FAILED_NO_CERTIFICATES error in 1.6 SDK. After some googling, I just renamed the file as, "close_btnnmb" and it worked fine. At that time, I came to know about Issue-830

So, when giving name to drawable files, try to avoid, two underscores. This is not a hard core rule, just my insight.

File Browser in Andriod

Image
File browser in android is used to access the internal file system in Android. Through the file browser, we can browse through the files. The features are

- sorting,
- size of the file
- an icon to recognize the file or directory
- open the file, if permission is there.

Then a Home button, to come to the root directory, SDcard button, to show sdcard files. A back button is always there, if the parent directory is not null.

For opening the file, there is function, I created called openFile(), in that, according to the extension of the file, I am setting the MIME type for intent using the API called setDataAndType(). The android file system contains lot of file types like audio, video, text, (text itself contain html, php, xml, csv etc).

Another important method(API), is listFiles(). This will return an array of files and this array, we are passing to the fill() in the FileBrowser class. We will check a file is a directory or not using isDirectory()


The code for openFile() which is an very i…

Launching Google Map using Intent in Android

Image
Using intent we can open google map. The URI for launching google map is geo:13.042206,80.17000?z=10

The complete code snippet is,



double latitude = 13.042206;
double longitude = 80.17000;
uri = Uri.parse("geo:" + latitude + "," + longitude +"?z=10");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);


The latitude and longitude , I specified here is for Chennai. We can also specify the zoom level. Here I specified the zoom level as 10.

The screenshot is,



From the menu you will get all the options that are available in google map as,

Vibration in Android

The code snippet for vibrating android phone is,


Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(2000);


Here 2000 is 2000 milliseconds, the phone will vibrate. And of course you can't test this in emulator, you need a real device to test this.

Opening an Application using Intent

In Android, if we know the package name and the activity which is launching the application, we can open or launch the application from anywhere using Intent.

The code snippet is,


Intent intent = new Intent("android.intent.action.MAIN");
ComponentName component = new ComponentName("com.sample", "com.sample.Sample");
intent.setComponent(component );
startActivity(intent);

Manufacturer Name and phone model in Android

To get the manufacturer name and other details about the android phone, we can use the class called android.os.Build

To get the manufacturer name use, Build.MANUFACTURER
To get the phone model use, Build.MODEL

Accelerometer Sensor in Android

Android phones has lot of sensors. In this, I am showing you the how accelerometer sensor works. Accelerometer will show you thew x, y and z co-ordinates of your phone movement. Actually, this sensor, we used in one of our application. In that, when shake the phone horizontally, doing some action. So like this, you can also do according to the values you get from the onSensorChanged() in SensorEventListener interface.

The complete code is,

AndAccelorometer.java public class AndAccelorometer extends Activity implements SensorEventListener { SensorManager sMgr; Sensor accelrometerSensor; @Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); sMgr = (SensorManager) getSystemService(Context.SENSOR_SERVICE); accelrometerSensor = sMgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sMgr.registerListener(this, accelrometerSensor, SensorManager.SENSOR_DELAY_UI); } public void onAccuracyChanged(Sensor sensor, int accuracy) { // TODO Auto-gener…

Opening Camera from Application

The basic things for augmented reality are camera, Sensor to know the direction, and the GPS or network provider to get the current location details. Gradually, I will show you the Augmented Reality in Android. In this, showing you how to open a camera from application.

CameraPreview.java



class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

SurfaceHolder holder;
Camera camera;

public CameraPreview(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public void surfaceChanged(SurfaceHolder holder2, int format, int w,
int h) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(w, h);
camera.setParameters(parameters);
camera.startPreview();
}

@Override
public void surfaceCreated(SurfaceHolder holder1) {
try {
camera = Camera.open();
camera.setPreviewDisplay(holder1);
} catch (Exception e) {
Log.i(&…

HTTP 100 Conitnue Error in Android

When I am doing a project in Android suddenly, I got this error "HTTP 100 conitnue" when sending request to server and I am not getting the proper response from the server. After some googling, I came to know about one API, and I want to share with all about that. It is setUseExpectContinue()

The complete code snippet is,


HttpProtocolParams.setUseExpectContinue(client.getParams(), false);

Here, client is an object of "HttpClient"

After using this code snippet, I got proper response from server and all work fine.

Happy Coding