Understanding Android - App Components
App Components
- Activities
- Broadcast Receivers
- Services
- Content Providers
Activities
- Represent a single screen with a user interface
- Performs actions on the screen
Broadcast Receivers
- BR simply responds to broadcast messages from other applications or from the system
- Ex. Battery low, Incoming Call, Incoming SMS, Wifi availability, Bluetooth connected, Charger disconnected (Get Notification)
Services
- Components that run in background to perform long-running operations
- Two kinds of base class you can extend to create Services: Service and IntentService class
- Ex: downloading a large file : show how much downloaded with notification, making http calls
public static final int START_STICKY_COMPATIBILITY = 0;
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), then leave it in the started state but
* don't retain this delivered intent. Later the system will try to
* re-create the service. Because it is in the started state, it will
* guarantee to call {@link #onStartCommand} after creating the new
* service instance; if there are not any pending start commands to be
* delivered to the service, it will be called with a null intent
* object, so you must take care to check for this.
*
* <p>This mode makes sense for things that will be explicitly started
* and stopped to run for arbitrary periods of time, such as a service
* performing background music playback.*/
public static final int START_STICKY = 1;
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), and there are no new start intents to
* deliver to it, then take the service out of the started state and
* don't recreate until a future explicit call to
* {@link Context#startService Context.startService(Intent)}. The
* service will not receive a {@link #onStartCommand(Intent, int, int)}
* call with a null Intent because it will not be restarted if there
* are no pending Intents to deliver.
*
* <p>This mode makes sense for things that want to do some work as a
* result of being started, but can be stopped when under memory pressure
* and will explicit start themselves again later to do more work. An
* example of such a service would be one that polls for data from
* a server: it could schedule an alarm to poll every N minutes by having
* the alarm start its service. When its {@link #onStartCommand} is
* called from the alarm, it schedules a new alarm for N minutes later,
* and spawns a thread to do its networking. If its process is killed
* while doing that check, the service will not be restarted until the
* alarm goes off.
*/
public static final int START_NOT_STICKY = 2;
/**
* Constant to return from {@link #onStartCommand}: if this service's
* process is killed while it is started (after returning from
* {@link #onStartCommand}), then it will be scheduled for a restart
* and the last delivered Intent re-delivered to it again via
* {@link #onStartCommand}. This Intent will remain scheduled for
* redelivery until the service calls {@link #stopSelf(int)} with the
* start ID provided to {@link #onStartCommand}. The
* service will not receive a {@link #onStartCommand(Intent, int, int)}
* call with a null Intent because it will only be restarted if
* it is not finished processing all Intents sent to it (and any such
* pending events will be delivered at the point of restart).
*/
public static final int START_REDELIVER_INTENT = 3;
Intent Service : @Depricated
- onHandleIntent()
Content Providers
- Supplies data from one application to others on request
- As per Google's inbuilt security model, app data is private to an application, hence it is not possible for an application to access another app's data by default.
- Content Providers acts as an interface for sharing data between apps.
- Content provider use standard insert(), query(), update() and delete() methods to access apps data.
- A special form of URI which starts with "content://" is assigned to each CP.
- Any app knowing this URI can insert(), update(), delete() and query() data from the database of the provider app.
- Example: Inbuilt SMS app in Android Devices is a classic example of CP.
- Any app can query the inbox from the device using its URI content://sms/inbox
- But READ_SMS permission must be declared in the app's AndroidManifest.xml file in order to access the SMS app's data.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.niranjan.mycoolapp">
<application
android:name=".MyCoolApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyCoolApp">
<activity android:name=".SavingActivity">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.niranjan.mycoolapp.services.MyService" />
<provider
android:authorities="com.niranjan.mycoolapp.providers.MyContentProvider"
android:name=".providers.MyContentProvider"
android:exported="false"
/>
<receiver android:name=".receivers.MyBroadcastReceiver" />
</application>
</manifest>
Build Gradle
- Project Level (Project: MyCoolApp)
- Module Level (Module: MyCoolApp.app)
- Module Level (Module: MyCoolApp.yourmodule)
// Add android specific build tasks
plugins {
id 'com.android.application'
id 'kotlin-android'
id "androidx.navigation.safeargs"
}
// configure android specific build options
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.my.mycoolapp"
minSdkVersion 22
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// Enable Data Binding
buildFeatures{
dataBinding true
viewBinding true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
// local binary dependencies
api project(":mockk")
// remote binary dependencies
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
// test configuration
// dependencies for local unit tests
testImplementation 'junit:junit:4.13.1'
}
Comments
Post a Comment