Giter Club home page Giter Club logo

androidserivce's Introduction

Service

This is a demo app on how to implement Service.

Key Points of Service:

  • In Android, a Service is an application component that can perform long-running operations in the background on the UI thread. By background, it means that it doesn’t have a user interface. A Service runs on the main thread of the calling Component’s process by default (and hence can degrade responsiveness and cause ANRs), hence you should create a new Thread to perform long running operations. A Service can also be made to run in a completely different process.
  • You cannot directly call public methods in a Service from an Activity without using a Binder. The Binder is used to create a connection between the Activity and the Service, allowing the Activity to communicate with the Service and call its public methods.

Start service

val intent = Intent(this, MyService::class.java)
intent.putExtra("myKey", "myValue")
startService(intent)

To declare a Service in your AndroidManifest.xml file, you can use the element. Here is an example:

<service
android:name=".MyService"
android:enabled="true"
android:exported="false" />
  • The android:enabled attribute is used to indicate whether the Service is enabled or not. When android:enabled is set to true, the Service is enabled and can be started or bound to by other components in the application or by other applications.
  • If android:enabled is set to false, the Service is disabled and cannot be started or bound to by other components or applications. This is useful when you want to temporarily disable a Service without removing it from the manifest file.
  • By default, the android:enabled attribute is set to true. If you do not explicitly set the attribute in your manifest file, it will default to true.
  • When android:exported is set to "false", it means that the component is not available for use by other applications or components. This is the default value for this attribute, and it is generally a good practice to keep it set to "false" for components that do not need to be accessed by other applications or components.

Stop service

  • Calling stopService() does not stop the service immediately. When stopService() is called, the service is marked for termination and its onDestroy() method is called. However, the system may not immediately destroy the service, as there may still be ongoing work being done in the service.
  • The system will destroy the service as soon as it is no longer in use (i.e., all clients have called unbindService() if the service was bound, and there are no pending start requests if the service was started with startService()).

Return type of onStartCommand()

  • START_STICKY- It will tell the system to create a newest copy of the service, when available memory is sufficient to do, after it retains state and recovers from the low memory. In this process we will loose the results that might have calculated before.
  • START_REDELIVER_INTENT- It will tell the system to restart and regain the service after the crash and also redeliver the intents that were present at the time of crash happened.
  • START_NOT_STICKY- It will tell the system not to worry and bother about to restart the service, even when it is having sufficient available memory.

Note

  • Starting from Android 11 (API level 30), when an app is in the background and its processes are no longer in use, the system may stop the app's background services. This means that if the app has no running components or if its running components are no longer being used by the user, the system may decide to stop the app's processes in order to free up system resources and improve performance.

androidserivce's People

Watchers

Alampally Dilip Kumar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.