In Android it is possible to create a Service to do background tasks, etc by creating a subclass of Service. It order to use the Service it must be specified in the manifest for the app:
All services must be represented by elements in the manifest file. Any that are not declared there will not be seen by the system and will never be run.
One of the parameters for a Service in the manifest is the 'enabled' option:
Whether or not the service can be instantiated by the system — "true" if it can be, and "false" if not.
What is the purpose in declaring a Service to be disabled - if I didn't want the Service surely I just wouldn't write it / add it to the manifest in the first place?
The only use I can see for disabling a Service in the manifest, and it seems of limited value, is if it's a Service used only for debugging, and I want it disabled for production. Am I missing something?
The android:enabled attribute set to a boolean value defined in a resource file. The purpose of this attribute is to enable or disable the service on devices running on different Android OS version.
For example, to disable the service on devices running Android 4.3 or lower include menifest attribute android:enabled="#bool/atLeastKitKat".
In addition to including this attribute in the manifest, you need to do the following:
In your bool.xml resources file under res/values/, add this line:
<bool name="atLeastKitKat">false</bool>
In your bool.xml resources file under res/values-v19/, add this line:
<bool name="atLeastKitKat">true</bool>
if I didn't want the Service surely I just wouldn't write it / add it to the manifest in the first place?
In the very specific case of a Service, I agree that it would be rare for you to want to disable it. One possibility would be for a service that plugs into the system (e.g., input method editor, accessibility service) that you only want to enable at runtime (via PackageManager and setComponentEnabledSetting()) if the user make an in-app purchase that unlocks the feature. I am sure that there are other Service scenarios for this, though none are leaping to mind at this early hour of the day (yawn!).
However, I suspect that Service "inherits" its android:enabled setting by virtue of being one of the Android component types, along with activities, providers, and receivers. Other scenarios for android:enabled will be a bit more common with other component types. For example, it is considered good form to have your BOOT_COMPLETED receiver be disabled until you know that you need it. So, for example, if the BOOT_COMPLETED receiver is only used to resume a download interrupted by a reboot, you only need that receiver enabled if you are doing a download. At all other times, you may as well leave it disabled, so you don't waste the user's time during "normal" reboots.
Related
How many processes may be in one android app?
We can added more than one process using manifest in android. What is max count?
According to android docs from here:
By default, every app runs in its own Linux process. The Android system starts the process when any of the app's components need to be executed, and then shuts down the process when it's no longer needed or when the system must recover memory for other apps.
and this:
By default, all components of the same application run in the same process and most applications should not change this. However, if you find that you need to control which process a certain component belongs to, you can do so in the manifest file.
The manifest entry for each type of component element <activity>, <service>, <receiver>, and <provider> supports an android:process attribute that can specify a process in which that component should run. You can set this attribute so that each component runs in its own process or so that some components share a process while others do not. You can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.
The <application> element also supports an android:process attribute, to set a default value that applies to all components.
we can find that each android application has one process by default. In addition by running each component in a separate process, the processes count would be equal to NUMBER_OF_COMPONENTS + 1 at max. There is no documentation about maximum possible count of processes for each android application.
I am creating an app where I need to collect status bar notifications. Users are prompted to allow either my implementation of NotificationListenerService (API >= 18) or AccessibilityService (other devices) and they are redirected to settings screen.
When I am on API < 18 the user is redirected to Accessibility settings screen, he allows the Accessibility service and everything is OK. However, when the user is on 18>=, even if the user is redirected to Notification settings he still can navigate to Accessibility settings to allow also the Accessibility Service. Both of my services are then registering notifications and notifying me about that.
Obviously I can check from which service the message is coming and react accordingly but I would prefer some cleaner solution. I don't want the user to be able to allow both services (they both appear in settings).
Is there a way to do something like defining separate manifest files for different API levels or declare <uses-sdk> inside <application> tag so they will be used for different API levels? And of course, we cannot create services programmatically - we have to declare them in manifest.
Step #1: Create a boolean resource in res/values/, named is18, set to false, and a second boolean resource named isLessThan18, set to true.
Step #2: Create a boolean resource in res/values-v18/, named is18, set to true, and a second boolean resource named isLessThan18, set to false.
Step #3: Use android:enabled="#boolean/is18" for your <service> element for your NotificationListenerService.
Step #4: Use android:enabled="#boolean/isLessThan18" for your <service> element for your AccessibilityService.
This will enable only one service per device, with the proper one dictated by the API level.
We specify current activity and start activity for an intent and we call it through the method startService(). So why do we need to mention that activity in manifest again?
Indexing (what can your app do?)
I can answer at least part of the why for Activities. The manifest is also where you declare your IntentFilter which is how the system understands what your application does. i.e. should your activity be an illegible choice when the user is trying to take a picture? choose a file? share a piece of text? In addition to that the IntentFilter also tells the Launcher application that you would like to have your activity included in the Applications drawer.
Configuring (what is your main activity?)
There are also several configuration options that you can set on Activities which have to be done in the manifest i.e. SingleTop. Without the declaration in the manifest there would be no place to declare these configurations.
Time Saving (where can the system find your service?)
The manifest file is used by the system to know what kind of components do the application have. Without registering your Activities/Services/Receivers/Content Providers the system would have to scan and parse the whole apk every time someone wants to use a specific component to find it. This would be really slow, that's why there is the AndroidManifest.xml, which is a small file, and it can be parsed fast to find the required component.
Sources: Why do Activities/Services need to be explicitly added to the Android manifest? why acitivies have to be registered in manifest file
Each android application you build will include a file called
AndroidManifest.xml which is placed in the root of the project
hierarchy. So why is it important? Because it lets you define the
structure and metadata of your android application and its components.
http://simpledeveloper.com/android-application-manifest-file/
we worked for exam with our friends. I tried to explain manifest file, talking about how to do things. But I see that I did know why to register activities to androidManifest.xml. Still do not know :). Does anyone have an idea?
The manifest file is used by the system to know what kind of components do the application have. Without registering your Activities/Services/Receivers/Content Providers the system would have to scan and parse the whole apk every time someone wants to use a specific component to find it. This would be really slow, that's why there is the AndroidManifest.xml, which is a small file, and it can be parsed fast to find the required component.
Manifest file describes the following features...
Version Number and Version Code:
This is useful, when you are uploading the application in Google play or go for upgrade the existing published apk.
2.minSdk and targetSdk:
mentioned your min and max version number your application supports.
3.application tag:
used to set the first activity or home activity when you launch an application from laucher.
4.activity tag:
the list of all activities in the application declared as child tag in application tag for the easier navigation to activity manager.
When we switch one activity to another activity, the activity manager checks whether this activity is declared in manifest file or not. If not found throws exception.
Uses: Developer can have look at all the activities at a glance (By Manifest file)
5.Filter tags: By intent filters in activity tag, User can open any kind of application activity.
Uses permission and Uses features:
All application resources are declared here.
example: Internet connectivity is needed for your application, Uses WIFI in your application.
I want to implement a service which should be running like standard system service on boot up, this service should not be kill-able and should be able to perform action on receiving notification from another process.
Can anyone help me which is the best methodology (AIDL) to create such service,if any example for reference ?
You can't do this unless you are creating your own system ROM.
If creating your own ROM, you can start by modifying the AndroidManifest of the apk containing your service. You need to add an attribute to your manifest node: android:sharedUserId="android.uid.system". That will cause your APK to hold the system ID (which requires the APK to be signed with your platform signing key -- this is why you need to be creating your own system ROM.
That will allow your application to be considered special by the system, and (at least on 4.x, I haven't tested on older Android versions) your application will be auto-started. The application being auto-started doesn't mean much on its own though; either you need to implement a BOOT_COMPLETED receiver as #febinkk suggests, or you can provide a custom Application override (by adding the attribute android:name="your.package.ApplicationSuperClass" to your application node in your AndroidManifest.xml). In your application super class, you can overload onCreate() and have it start your service or whatever else is required.
Additionally, as a system application, I believe (though have not fully tested) you will not be able to be killed through normal means.
You are not able to create non-killable, immune service without creating your own ROM
You could register a BroadcastReciever with filter for android.intent.action.BOOT_COMPLETED for your service and after starting call startForeground(). This may not be what exactly you were looking, but this is probably the only thing that comes near, if you don't want to create ROM.