Service Lifecycle Management
When a Service is started via startService(), the system automatically invokes onCreate() followed by onStartCommand(). If the same Service is started multiple times using startService(), onCreate() is called only once.
To stop a Service initiated with startService(), call stopService(), which triggers onDestroy(). However, if a Service is both started and bound, stopService() will not terminate it unless all bindings are first released with unbindService().
Binding to a Service with bindService() results in calls to onCreate() and onBind(). Releasing the binding with unbindService() invokes onUnbind() and onDestroy().
Key distinctions: startService() and stopService() merely start and stop a Service without allowing direct interaction, and the Service persists after the caller exits. In contrast, bindService() and unbindService() enable enteraction with the Service, and the Service is destroyed when the caller unbinds and terminates.
Animation in Android
Android supports three primary animation types: frame animation, tween animation, and property animation (introduced in Android 3.0).
Frame Animation
Frame animation relies on a sequence of images played in rapid succession to create motion. Its straightforward to implement but requires prepared UI assets. The attribute android:oneshot controls whether the animation runs once (true) or loops (false).
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/frame1" android:duration="100" />
<item android:drawable="@drawable/frame2" android:duration="100" />
</animation-list>
Tween Animation
Tween animations include four transformations: alpha (fade), translate (movement), scale (resizing), and rotate (rotation). They are typically defined in XML for reusability and clarity. The Interpolator adjusts the animation's pace, while pivot sets the reference point for transformations.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500" />
</set>
Property Animation
Property animation allows dynamic modification of object properties over time, offering more flexibility than tween animations by enabling changes to any property of any object.
Core Android Components
- Activity
- Broadcast Receiver
- Content Provider
- Service
Common Layouts in Android
Push Notification Methods
Data Storage in Android
- SharedPreferences: Stores simple configuration data in XML format, accessible only within the same application package.
- File Storage: Uses standard Java I/O methods with Android-specific helpers like
openFileInput()andopenFileOutput()for device file access. - SQLite Database: A lightweight, embedded database supporting SQL queries for structured data storage.
- ContentProvider: Facilitates data exchange between applications, allowing shared access to data sets.
- Network Storage: Stores and retrieves data from remote servers via internet protocols.