A Short Note – Service vs IntentService In Android

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (Service vs IntentService In Android).

In this note series, we will understand the differences between the Service and IntentService in Android.

So Let’s begin.

Service

Service is an android component that is used to perform some long-running operations in the background, such as in the Music app, where we run the app in the background while using other mobile apps at the same time. The best part is that we don’t need to provide some UI for the operations to be performed in the background. By using Service, we can perform some InterProcess Communication(IPC) also. So, with the help of Service, we can perform a number of operations together because any application component can start a Service and can run in background.

There are three ways of using Service:

1. Foreground Service :

A foreground service is a Service that will let the user know about what is happening in the background. For example, in the Music application, the user can see the ongoing song on the device as a form of notification. So, here displaying notification is a must.

2. Background Service :

Here, the user will never know about what is happening in the background of the application. For example, whatsapp messenger compresses the image file to reduce the size while sending some images over whatsapp. This task is done in background and the user have no idea about what is going in the background. But for the API level 21 or higher, the Android System imposes some restrictions while using the Background Service. So, we take care of those restrictions before using the Background Service.

3. Bound Service :

The Bound Service is used when one or more than one application component binds the Service by using the bindService() method. If the applications unbind the Service, then the Service will be destroyed.

IntentService

The Service is the base class for the IntentService. Basically, it uses “work queue process” pattern where the IntentService handles the on-demand requests (expressed as Intents) of clients. So, whenever a client sends a request then the Service will be started and after handling each and every Intent, the Service will be stopped. Clients can send the request to start a Service by using Context.startService(Intent) . Here, a worker thread is created and all requests are handled using the worker thread but at a time, only one request will be processed.

To use IntentService, we have to extend the IntentService and implement the onHandleIntent(android.content.Intent).

Service vs IntentService

In this section, we will look upon some of the differences between the Service and IntentService, so that it will be easier for us to find which one to use in which condition. Let’s see the difference:

  • If we want some background task to be performed for a very long period of time, then we should use the IntentService. But at the same time, we should take care that there is no or very less communication with the main thread. If the communication is required then we can use main thread handler or broadcast intents. We can use Service for the tasks that don’t require any UI and also it is not a very long running task.
  • To start a Service, we need to call the onStartService() method while in order to start IntentService, we have to use Intent i.e. start the IntentService by calling Context.startService(Intent).
  • Service always runs on the Main thread while the IntentService runs on a separate Worker thread that is triggered from the Main thread.
  • Service can be triggered from any thread while the IntentService can be triggered only from the Main thread i.e. firstly, the Intent is received on the Main thread and after that, the Worker thread will be executed.
  • If we are using Service then there are chances that our Main thread will be blocked because Service runs on the Main thread. But, in case of IntentService, there is no involvement of the Main thread. Here, the tasks are performed in the form of Queue i.e. on the First Come First Serve basis.
  • If we are using Service, then we have to stop the Service after using it otherwise the Service will be there for an infinite period of time i.e. until our phone is in normal state. So, to stop a Service, we have to use stopService() or stopSelf() . But in the case of IntentService, there is no need of stopping the Service because the Service will be automatically stopped once the work is done.
  • If we are using IntentService, then we will find it difficult to interact with the UI of the application. If we want to out some result of the IntentService in our UI, then we have to take help of some Activity.

Conclusion

In this note series, we understood about Service and IntentService differences and usages in android. We also discussed about fundamental concepts of Service and IntentService. If we have some limited amount of tasks to be performed in the background, then we can use Service, otherwise, we can use IntentService.

Thanks for reading! I hope you enjoyed and learned about Service vs IntentService in Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

A Short Note – An Overview Of Functions Currying In Kotlin

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (An Overview Of Functions Currying In Kotlin).

In this note series, We will understand about Functions Currying in Kotlin. Currying is a common technique in functional programming. It allows transforming a given function that takes multiple arguments into a sequence of functions, each having a single argument.

In this short note series, we are going to implement an automatic currying mechanism that could be applied to any function taking three parameters.

So Let’s begin.

Overview

We can understand Currying as :

  • A common technique in functional programming.
  • Transforming a given function that takes multiple arguments into a sequence of functions, each having a single argument.
  • Each of the resulting functions handles one argument of the original (uncurried) function and returns another function. 

To understand the concept of functions currying, let’s consider the following example of a function handling three parameters:

fun foo(a: A, b: B, c: C): D 

Its curried form would like this :

fun carriedFoo(a: A): (B) -> (C) -> D 

In other words, the curried form of the foo function would take a single argument of the A type and return another function of the following type: (B) -> (C) -> D. The returned function is responsible for handling the second argument of the original function and returns another function, which takes the third argument and returns a value of type D. 

How To Implement it ?

In this section, we are going to implement the curried() extension function for the generic functional type declared as follows:  ((P1, P2, P3). The curried() function is going to return a chain of single-argument functions and will be applicable to any function which takes three arguments.

Here, we can implement Functions Currying with the below steps :

Step 1 – Declare a header of the curried() function :

fun<P1, P2, P3, R>((P1, P2, P3) -> R).curried(): (P1) -> (P2) -> (P3) -> R

Step 2 – Implement the curried() function body :

fun<P1, P2, P3, R>((P1, P2, P3) -> R).curried(): (P1) -> (P2) -> (P3) -> R =
{ p1: P1 ->
 { p2: P2 ->
  { p3: P3 ->
    this(p1, p2, p3)
  }
 }
}

How To Works ?

Let’s explore how to use the curried() function in action. In the following example, we are going to call curried() on the following function instance which is responsible for computing a sum of three integers:

fun sum(a: Int, b: Int, c: Int): Int = a + b + c

In order to obtain a curried form of the sum() function, we have to invoke the curried() function on its reference:

::sum.curried()

Then we can invoke the curried sum function in the following way:

val result: Int = ::sum.curried()(1)(2)(3)

Here, the result variable is going to be assigned an integer value equal to 6.

In order to invoke the curried() extension function, we access the sum() function reference using the :: modifier. Then we invoke the next functions from the function sequence returned by the curried function one by one. 

The preceding code could be written in an equivalent more verbose form with explicit types declarations:

val sum3: (a: Int) -> (b: Int) -> (c: Int) -> Int = ::sum.curried()
val sum2: (b: Int) -> (c: Int) -> Int = sum3(1)
val sum1: (c: Int) -> Int = sum2(2)
val result: Int = sum1(3)

Under the hood, the currying mechanism implementation is just returning functions nested inside each other. Every time the specific function is invoked, it returns another function with the arity reduced by one.

Conclusion

In this note series, we understood about Functions Currying in Kotlin. Currying is useful whenever we can’t provide the full number of required arguments to the function in the current scope. We can apply only the available ones to the function and return the transformed function.

Thanks for reading! I hope you enjoyed and learned about Functions Currying in Kotlin. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

A Short Note – An Overview Of Android Foreground Service Launch Restrictions

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (An Overview Of Android Foreground Service Launch Restrictions).

In this note series, We will understand about Android Foreground Service Launch Restrictions in Android 12.

Apps that target Android 12 can no longer start foreground services while running in the background, except for a few special cases. If an app tries to start a foreground service while the app is running in the background, and the foreground service doesn’t satisfy one of the exceptional cases, the system throws a ForegroundServiceStartNotAllowedException.

So Let’s begin.

Recommended alternative to foreground services

If our app is affected by this change, we can migrate to using WorkManager. WorkManager is the recommended solution for starting higher-priority background tasks.

Starting in WorkManager 2.7.0, our app can call setExpedited() to declare that a Worker should use an expedited job. This new API uses expedited jobs when running on Android 12, and the API uses foreground services on prior versions of Android to provide backward compatibility.

The following code snippet shows an example of how to use the setExpedited() method:

OneTimeWorkRequestBuilder<T>().apply {
    setInputData(inputData)
    setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
}.build()

Because the CoroutineWorker.setForeground() and ListenableWorker.setForegroundAsync() methods are backed by foreground services, they’re subject to the same foreground service launch restrictions and exemptions. We can use the API opportunistically, but be prepared to handle an exception if the system disallows our app from starting a foreground service. For a more consistent experience, use setExpedited().

Cases where foreground service launches from the background are allowed

In the following situations, our app can start foreground services even while our app is running in the background:

  • Our app transitions from a user-visible state, such as an activity.
  • App can start an activity from the background, except for the case where the app has an activity in the back stack of an existing task.
  • Our app receives a high-priority message using Firebase Cloud Messaging.
  • The user performs an action on a UI element related to our app. For example, they might interact with a bubble, notification, widget, or activity.
  • Our app receives an event that’s related to geofencing or activity recognition transition.
  • After the device reboots and receives the ACTION_BOOT_COMPLETEDACTION_LOCKED_BOOT_COMPLETED, or ACTION_MY_PACKAGE_REPLACED intent action in a broadcast receiver.
  • Our app receives the ACTION_TIMEZONE_CHANGEDACTION_TIME_CHANGED, or ACTION_LOCALE_CHANGED intent action in a broadcast receiver.
  • App receives a Bluetooth broadcast that requires the BLUETOOTH_CONNECT or BLUETOOTH_SCAN permissions.
  • Apps with certain system roles or permission, such as device owners and profile owners.
  • Our app uses the Companion Device Manager. To let the system wake our app whenever a companion device is nearby, implement the Companion Device Service in Android 12.
  • The system restarts a “sticky” foreground service. To make a foreground service sticky, return either START_STICKY or START_REDELIVER_INTENT from onStartCommand().
  • The user turns off battery optimizations for your app. We can help users find this option by sending them to our app’s App info page in system settings. To do so, invoke an intent that contains the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent action.

Conclusion

In this note series, we understood about Android Foreground services launch restrictions in Android 12. We discussed about cases where foreground service launches from the background are allowed.

Thanks for reading! I hope you enjoyed and learned about Foreground service launch restrictions in Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

A Short Note – How Does The MVI Work In Android ?

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (How does the MVI work in Android?).

In this note series, We will understand what MVI architectural pattern is, how it resolves these challenges in android.

Android architectural patterns are growing day by day. As we develop apps, we face new challenges and issues. New patterns will be discovered as we keep solving similar challenges. As Android Developers, we have MVC, MVP, and MVVM as the most commonly used patterns. All of them use an imperative programming approach. With this approach, even though most of our challenges will be resolved, we still face some challenges regarding the thread safety, maintaining states of the application.

So Let’s begin.


What is MVI architecture?

MVI stands for Model-View-Intent. This pattern has been introduced recently in Android. It works based on the principle of unidirectional and cylindrical flow inspired by the Cycle.js framework.

Let’s see what is the role of each component of MVI.

  • Model: Unlike other patterns, In MVI Model represents the state of the UI. i.e. for example UI might have different states like Data Loading, Loaded, Change in UI with user Actions, Errors, User current screen position states. Each state is stored as similar to the object in the model.
  • View: The View in the MVI is our Interfaces, which can be implemented in Activities and fragments. It means to have a container which can accept the different model states and display it as a UI. They use observable intents(Note: This doesn’t represent the Android traditional Intents) to respond to user actions.
  • Intent: Even though this is not an Intent as termed by Android from before. The result of the user actions is passed as an input value to Intents. We can say we will send models as inputs to the Intents, which can load it through Views.


How does the MVI work?

User does an action which will be an Intent → Intent is a state which is an input to model → Model stores state and send the requested state to the View → View Loads the state from Model → Displays to the user.

If we observe, the data will always flow from the user and end with the user through intent. It cannot be the other way, hence its called Unidirectional architecture. If the user does one more action, the same cycle is repeated, hence it is Cyclic.


Advantages and Disadvantages of MVI

Let’s see what are the advantages and disadvantages of MVI.


Advantages of MVI

  • Maintaining state is no more a challenge with this architecture, As it focuses mainly on states.
  • As it is unidirectional, Data flow can be tracked and predicted easily.
  • It ensures thread safety as the state objects are immutable.
  • Easy to debug, As we know the state of the object when the error occurred.
  • It’s more decoupled as each component fulfills its own responsibility.
  • Testing the app also will be easier than we can map the business logic for each state.


Disadvantages of MVI

  • It leads to lots of boilerplate code as we have to maintain a state for each user action.
  • As we know it has to create lots of objects for all the states. This makes it too costly for app memory management.
  • Handling alert states might challenge while we handle configuration changes. For example, if there is no internet we will show the snackbar, On configuration change, it shows the snackbar again as it is the state of the intent. In terms of usability, this has to be handled.


Conclusion

In this note series, we understood about what MVI architectural pattern is, how it resolves these challenges in android.

Thanks for reading! I hope you enjoyed and learned about MVI Concept in Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

A Short Note – R8 vs Proguard In Android

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series (R8 vs Proguard In Android).

In this note series, we will understand about R8 vs Proguard in Android. Google released R8 as a replacement of Proguard to help developers shrink the code with the better-generated output (APK). They are considered much faster compared to Proguard.

So Let’s begin.

What is R8?

R8 is a tool that converts our java byte code into an optimized dex code. It iterates through the whole application and then it optimizes like removing unused classes, methods, etc. and runs on the compile time. It helps us to reduce the size of the build and to make our app to be more secure. R8 uses Proguard rules to modify its default behavior.

How does R8 shrinking work?

While optimizing the code, R8 reduces the code of our application, and then APK size is reduced.

To reduce the APK size, we have three different techniques:

  1. Shrinking or Tree Shaking: Shrinking is the process of removal of unreachable code from our Android project. R8 performs some static analysis to get rid of unreachable code and removes the un-instantiated object.
  2. Optimization: This is used to optimize the code for size. It involves dead code removal, unused argument removal, selective in-lining, class merging, etc.
  3. Identifier Renaming: In this process, we obfuscate the class name and other variable names. For example, if the name of the class is “MainActivity“, then it will be obfuscated to “a” or something else but smaller in size.

How to enable R8 Shrinking in our app?

R8 is present by default in our application but to enable R8 shrinking in our application, set the minifyEnabled to true in our app’s main build.gradle file.

android {
    ...
    buildTypes {
        release {
            minifyEnabled true
        }
    }
}

R8 vs Proguard

So, let’s now compare both R8 and Proguard both and see how it fares,

  • With the Android app using Gradle plugin above 3.4.0 or more the project uses R8 by default and no longer uses the Proguard to perform optimizations. But, it uses Proguard rules only.
  • R8 inlines the container classes and removes unused class, fields, and methods. Proguard reduces the app size by 8.5% and compared to R8 which reduces the code by 10%.
  • R8 has more Kotlin support compared to Proguard.
  • R8 gives better outputs than Proguard, and to do so faster than Proguard does, reducing overall build time.

So, let’s now compare how both Proguard and R8 performs.

Proguard

While using Proguard, it converts the Applications code to Java byte-code by the Java compiler. After the conversion, it is then optimized by Proguard using the rules which we have written. Then dex converts it to optimized Dalvik byte code.

This is roughly a 4 step process to convert it to Dalvik byte-code.

R8

While using R8, first the app’s code is converted to Java byte-code by the java compiler and then using R8 directly, it converts the java byte-code in Dalvik byte-code.

By using R8, it directly reduces the steps of conversion of Java byte-code to Dalvik Byte-code from 2 to 1.

  • Proguard applies 520 peephole optimizations compared to R8, which is very less. Peephole optimizations are the optimizations that are performed on a set of compiler-generated code to improve the performance of the code by making it shorter and faster.
  • In both Proguard and R8, we have to handle the reflection by writing the custom configuration.
  • R8 is faster compared to Proguard in the execution of converting the code.

Optimization Comparison between Proguard and R8

Let us discuss a few features supported by both Proguard and R8.

For example, Proguard and R8 both make the methods private in the code. Both of them also remove unused class, fields, or even methods in the project are not in use. Both of them support the simplification of Enum types. They both inline methods, merge codes, etc.

Proguard also makes the classes final whereas R8 cannot do it. But comparing R8, which is highly supported by Kotlin, optimizes the Kotlin construct which is not possible with Proguard.

Now, If you also want to enable aggressive optimization in R8 and reduce the size more, enable the following in gradle.properties.

android.enableR8.fullMode=true

Conclusion

In this note series, we understood about R8 vs Proguard in Android. We have also discussed R8 and Proguard process and optimization differences. With R8 coming as a default compile-time optimizer, it reduces the size of the app.

Thanks for reading! I hope you enjoyed and learned about R8 vs Proguard in Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

A Short Note – RxJava And RxAndroid For Android

Hello Readers, CoolMonkTechie heartily welcomes you in A Short Note Series.

Multi-threading in Android has always been an enormous task for engineers. RxJava has made life easy for all the Android developers using it. In this note series, we are going to discuss RxJava for Android specifically.


So Let’s begin.


What is RxJava?

RxJava is a JVM library for doing asynchronous and executing event-based programs by using observable sequences. Its main building blocks are triple O’s, Operator, Observer, and Observables. And using them we perform asynchronous tasks in our project. It makes multi-threading very easy in our project. It helps us to decide on which thread we want to run the task.

But RxJava is made for primarily any Java projects. To use RxJava in Android, we will also need RxAndroid.


What is RxAndroid?

RxAndroid is an extension of RxJava for Android, which is used only in Android application.

RxAndroid introduced the Main Thread required for Android.

To work with the multithreading in Android, we will need the Looper and Handler for Main Thread execution.

RxAndroid provides AndroidSchedulers.mainThread() which returns a scheduler and that helps in performing the task on the main UI thread that is mainly used in the Android project. So, here AndroidSchedulers.mainThread() is used to provide us access to the main thread of the application to perform actions like updating the UI.

In Android, updating UI from background thread is technically not possible, so using AndroidSchedulers.mainThread() we can update anything on the main thread. Internally it utilizes the concept of Handler and Looper to perform the action on the main thread.

RxAndroid uses RxJava internally and compiles it. But while using RxAndroid in our project we still add the dependency of RxJava to work with like,

implementation 'io.reactivex.rxjava3:rxjava:3.0.0'
implementation 'io.reactivex.rxjava3:rxandroid:3.0.0'


Use-Cases in Android

RxJava has the power of operators and as the saying goes by, “RxJava has an operator for almost everything“.


Case 1:

Consider an example, where we want to do an API call and save it to some storage/file. It would be a long-running task and doing a long-running task on the main thread might lead to unexpected behavior like App Not Responding.

So, to do the above-mentioned task, we might think to use AsyncTask as our go-to solution. But with Android R, AsyncTask is going to be deprecated, and then libraries like RxJava will be the solution for it.

Using RxJava over AsyncTask helps us to write less code. It provides better management of the code, as using AsyncTask might make the code lengthy and hard to manage.


Case 2:

Consider a use-case where we might want to fetch user details from an API and from the user’s ID which we got from the previous API we will call another API and fetch the user’s friend list.

Doing it using AsyncTask we might have to use multiple Async task and manage the results in was way where we want to combine all the AsyncTask to return the result as a single response.

But using RxJava, we can use the power of zip operator to combine the result of multiple different API calls and return a single response.


Case 3:

Consider an example of doing an API call and getting a list of users, and from that, we want only the data which matches the given current condition.

A general approach is to do the API call, and from the Collection, we can then filter the content of that specific user based on the condition and then return the data.

But using RxJava we can directly filter out the data while returning the API response by using the filter operator, and we do all of this by doing the thread management.

These are a few use cases to understand RxJava for Android and why we need RxAndroid in our project.


Conclusion

In this note series, we understood about the RxJava and RxAndroid for Android. We discussed a few use cases to understand RxJava for Android and why we need RxAndroid in our project.

Thanks for reading ! I hope you enjoyed and learned about the RxJava and RxAndroid Concepts for Android. Reading is one thing, but the only way to master it is to do it yourself.

Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.

You can find Other articles of CoolmonkTechie as below link :

You can also follow official website and tutorials of Android as below links :

If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.

Thanks again Reading. HAPPY READING !!???

Exit mobile version