Thursday, March 24, 2011

Event, AIDL, Screen Resolution and Orientation

1) How many types of Events in Android ?
Answer :-

onKeyDown :-  onKeyDown Called when any device key is pressed; includes the D-pad, keyboard, hang-up, call, back, and camera buttons.
@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
     // Return true if the event was handled.
     return true;
}
onKeyUp :- onKeyUp Called when a user releases a pressed key.
@Override
public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {
     // Return true if the event was handled.
     return true;
}
onTrackballEvent :- onTrackballEvent Called when the device’s trackball is moved
@Override
public boolean onTrackballEvent(MotionEvent event ) {
     // Get the type of action this event represents
     int actionPerformed = event.getAction();
     // Return true if the event was handled.
     return true;
}
onTouchEvent :- onTouchEvent Called when the touch screen is pressed or released, or it detects movement.
@Override
     public boolean onTouchEvent(MotionEvent event) {
     // Get the type of action this event represents
     int actionPerformed = event.getAction();
    // Return true if the event was handled.
    return true;
}

2) What is Sandboxing ?
Answer :-
 Each application in Android runs in its own process. An application cannot directly access another application's memory space. This is called application sandboxing.

3) What is AIDL in Android ? How we achieve Interprocess communication (IPC) protocol in Android ?
Answer :-
In order to allow cross-application communication, Android provides an implementation of Interprocess Communication (IPC) protocol. IPC protocols tend to get complicated because of all the marshaling/un marshaling of data that is necessary.
                        To help with this, Android provides Android Interface Definition Language, or AIDL. It is a lightweight implementation of IPC using a syntax that is very familiar to Java developers, and a tool that automates the stub creation.

4) How we handle multiple screen resolution in Android ?
Answer :-

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
...
</manifest>
The objective of supporting multiple screens is to create an application that can run properly on any display and function properly on any of the generalized screen configurations supported by the platform.
You can easily ensure that your application will display properly on different screens. Here is a quick checklist:
  • Use wrap_content, fill_parent, or the dp unit (instead of px), when specifying dimensions in an XML layout file 
  • Do not use AbsoluteLayout 
  • Do not use hard coded pixel values in your code 
  • Use density and/or resolution specific resources
5) How we manage Screen Orientation in Android ?
Answer :-

Changing the Screen Orientation Based on the Accelerometer :- If you want to change the screen orientation automatically based on the positioning of the device, you can use the android:screenOrientation attribute in the AndroidManifest.xml file:
<manifest> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<activity android:name=".Orientation" 
             android:screenOrientation="sensor"               
             android:label="@string/app_name"> 
<intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> 
</activity> 
</application> 
</manifest>
The above specifies that the screen orientation for the Orientation activity is based on the sensor (accelerometer) of the device. If you hold the device upright, the screen will be displayed in portrait mode; if you hold it sideways, it will change to landscape mode.

Changing the Screen Orientation Programmatically :- There are times where you need to ensure that your application is displayed only in a certain orientation. For example, suppose you are writing a game that should only be viewed in landscape mode. In this case, you can programmatically force a change in orientation using the setRequestOrientation() method of the Activity class: 
import android.app.Activity; 
import android.content.pm.ActivityInfo; 
import android.os.Bundle; 
public class Orientation extends Activity { 
  /** Called when the activity is first created. */ 
  @Override 
  public void onCreate(Bundle savedInstanceState) {     
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //---change to landscape mode---   
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);   
  } 

To change to portrait mode, use the ActivityInfo.SCREEN_ORIENTATION_PORTRAIT constant:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 

Besides using the setRequestOrientation() method, you can also use the android:screenOrientation attribute on the <activity> element in AndroidManifest.xml as follows to fix the activity to a certain orientation: 

<manifest> 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
<activity android:name=".UIActivity" android:screenOrientation="landscape" android:label="@string/app_name"> 
<intent-filter> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
</activity> 
</application> 
</manifest> 

The above example fixes the activity to a certain orientation (landscape in this case) and prevents the activity from being destroyed; that is, the activity will not be destroyed and the onCreate event will not be fired again when the orientation changes.


1 comment: