Posts

Showing posts from January, 2011

Playing Ringtone in Android

Android have some system ringtones. We can get the uri for these files through RingtoneManager class. Then using AudioManager and mediaplayer, we can play ringTone up on getting some notifications or for alarm etc.

Note : This code won't work on emulator, this will work only on devices. In emulator, we will get IOException

The complete code is,


try {
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(this, alert);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) {
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
mMediaPlayer.setLooping(true);
mMediaPlayer.prepare();
mMediaPlayer.start();
}
} catch(Exception e) {
}

Difference between SurfaceView and View in Android

The main difference between SurfaceView and View is,

- Views are drawn in UI thread and surfaceView are drawn in a seperate thread. So if rendering takes long time, its better to use SurfaceView. Then for animations also, its better to use surfaceView.

- SurfaceView uses more resoources than View, so use it when you really want.

Interprocess Communication in Android - Part I

Interprocess communication can be done in two ways in Android.

- Through AIDL(Android Interface Description Language)
- Through Service and Broadcast Receiver

First I am going to explain inter process communication through AIDL.

Android Interface Description Language(AIDL)

For implementing inter process communication(IPC) through AIDL, the main things that we need are,

- Create a AIDL file containing the functions, that we are going to implement in the Stub class in Service.

- Create a service, in which we need to implement all the the methods that we declare in the AIDL interface. Android will create a stub class and we need to define the methods here. Stub class is nothing but an object of the IBinder. IBinder is an interface for remote objects. It desribes the protocol for interacting with the remote objects.

- Then for calling the AIDL service, we need to implement ServiceConnection interface, for monitoring the state of the application service. This interface contains two method…

Fetching screen height, finding screen orientation

Here I am going to explain some simple but useful tips in Android.

Fetch Screen Height and width of our Android Phone
To fetch the screen height and width of our Android phone, the code is
DisplayMetrics displaymetrics = new DisplayMetrics(); context.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int screenWidth = displaymetrics.widthPixels; int screenHeight = displaymetrics.heightPixels;
To find the orientation of our Android Phone
To find out the orientation, that is whether it is in landscape, portrait or in any another orientation, the code is,

int orientation = getResources().getConfiguration().orientation; This will give the current orientation. To check whether it is in landscape or portrait, if (orientation == Configuration.ORIENTATION_PORTRAIT) { // do something } else if (orientation == Configuration.ORIENTATION_LANDSCAPE) { // do something else }