Q-3: Attempt the following (Any two) (10 Marks)
Questions
A: Differentiate between DVM and JVM.
B: State syntax to create Text View and Image button with any two attributes of each.
C: Describe Android service life cycle along with diagram.
Answer A: Differentiate between DVM and JVM
Aspect | DVM (Dalvik Virtual Machine) | JVM (Java Virtual Machine) |
---|---|---|
Full Form | Dalvik Virtual Machine | Java Virtual Machine |
Architecture | Register-based | Stack-based |
File Format | .dex (Dalvik Executable) | .class files |
Memory Usage | Less memory consumption | More memory consumption |
Performance | Faster execution | Slower compared to DVM |
Platform | Android specific | Platform independent |
Optimization | Optimized for mobile devices | General purpose optimization |
Multiple Instances | Multiple DVM instances can run | Single JVM instance per application |
Bytecode | Dalvik bytecode (.dex) | Java bytecode (.class) |
Garbage Collection | Concurrent garbage collector | Various GC algorithms |
Purpose | Mobile application execution | Desktop/server applications |
Resource Constraints | Designed for limited resources | Not specifically resource-constrained |
Key Differences:
- DVM uses register-based architecture making it more efficient for mobile devices
- JVM uses stack-based architecture which is more traditional
- DVM runs .dex files while JVM runs .class files
- DVM is optimized for Android while JVM is platform-independent
Answer B: Syntax for TextView and ImageButton
TextView Syntax with Attributes
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:textSize="18sp"
android:textColor="#000000"
android:background="#FFFFFF"
android:padding="10dp"
android:gravity="center" />
Two Key Attributes for TextView:
- android:text - Sets the text content to display
- android:textSize - Sets the size of the text (in sp units)
ImageButton Syntax with Attributes
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_button"
android:background="@drawable/button_background"
android:contentDescription="Image Button"
android:scaleType="fitCenter"
android:padding="8dp"
android:onClick="imageButtonClick" />
Two Key Attributes for ImageButton:
- android:src - Sets the image/icon to display on the button
- android:background - Sets the background drawable or color for the button
Additional Common Attributes:
- android:id - Unique identifier
- android:layout_width/height - Dimensions
- android:onClick - Click event handler
- android:padding - Internal spacing
Answer C: Android Service Life Cycle with Diagram
Android Service Life Cycle
A service is a component that runs in the background to perform long-running operations without user interface.
Service Life Cycle Methods:
- onCreate() - Called when service is first created
- onStartCommand() - Called when service is started using startService()
- onBind() - Called when service is bound using bindService()
- onUnbind() - Called when all clients unbind from service
- onRebind() - Called when service is rebound after unbinding
- onDestroy() - Called when service is destroyed
Service Life Cycle Diagram:
Service Created
|
onCreate()
|
┌─────────────────┐
│ │
▼ ▼
startService() bindService()
│ │
▼ ▼
onStartCommand() onBind()
│ │
▼ │
Service Running │
│ │
▼ ▼
stopService() unbindService()
│ │
▼ ▼
Service Stopped onUnbind()
│ │
└─────────┬───────┘
▼
onDestroy()
│
▼
Service Destroyed
Types of Services:
-
Started Service (using startService())
- Runs indefinitely in background
- Must stop itself or be stopped by another component
-
Bound Service (using bindService())
- Provides client-server interface
- Runs only while bound to components
Service States:
- Created: Service object created but not running
- Started: Service running after startService() call
- Bound: Service bound to client components
- Destroyed: Service stopped and resources released
Example Service Implementation:
public class MyService extends Service {
@Override
public void onCreate() {
super.onCreate();
// Initialize service
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Service logic here
return START_STICKY; // Restart if killed
}
@Override
public IBinder onBind(Intent intent) {
return null; // Return null for started service
}
@Override
public void onDestroy() {
super.onDestroy();
// Cleanup resources
}
}