How run other process to background? - android

Is it possible to run other application (process) to background? I mean that for example I will use intent for starting some other application, but I don't want to make this application running in foreground but in background, it means that application shouldn't be opened but only kept in memory minimized.

If the other Application has an accessible service, you can call startService with an intent that references that service. This will load the Application if it is not already running.
By "accessible" I mean the manifest for the other application should contain a
<service android:exported=true" ... /> element

You need use service (or intentService) to run something in the background. If you need to display info from it have an activity get its data from the service. if you only use an activity it will be paused and stopped according to the android lifecycle

Related

How to make sure Service gets called from a specific activity

In android how to make sure that the service that I have in the application will get called only through an activity that is within the app.
(In other words I want to limit the service to get played by only a certain activity & not even by other activities within that app)
I tried studying Intent-filters but got a bit confused.
Can someone please suggest, if possible with an example?
Thank You
Use Context.startService(Intent service) to start the service from your activity.
Despite the name it doesnt only start the service. If the service is running already, it just calls it.
From the service perspective, the service will then call its onStartCommand(...) method.
Only an activity within the same application can call/start a service this way.
If you set exported="false" in your <service .. /> element of AndroidManifest.xml, the service cannot be called by activities outside your own app.
I know of no way to limit access to any particular activity within the app, but this seems a less pressing concern. Supposedly you can trust your own code?

Can you start an IntentService on a separate process?

Is it possible to start an IntentService on a separate process? How? If so, is it mandatory to bind to it?
Is it possible to start an IntentService on a separate process AND run it in the foreground?
What's the difference between android:isolatedProcess and android:process? See: http://developer.android.com/guide/topics/manifest/service-element.html
1) Is it possible to start an IntentService on a separate process? How? If so, is it mandatory to bind to it?
Yes, you can start an IntentService in a separate process. Simply add android:process=":whatever" to the manifest entry for that service.
No, you don't need to bind to it. You can communicate with it by sending it Intents using startService()
2) Is it possible to start an IntentService on a separate process AND run it in the foreground?
Yes (see above). To make your service run in the foreground it can call startForeground() whenever it wants to do that. The service itself is in control of whether it runs in the foreground or background.
3) What's the difference between android:isolatedProcess and android:process? See: http://developer.android.com/guide/topics/manifest/service-element.html
android:process allows you to control in which process each particular component runs (by specifying the name of the process). You can group components of your application to run in separate processes (for example, all UI components in one process and all services in another). The default behaviour is that all components of an application run in the same process.
android:isolatedProcess is a flag (true/false) that you can set if you want a particular service component to run in a separate process isolated from the rest of your application. The isolated process doesn't have any of the permissions that are granted to the rest of your application. Normally, permissions are granted to an application and all components of the application have all the permissions that the application gets. android:isolatedProcess is only available starting with API level 16 (Jellybean). See http://aleksmaus.blogspot.de/2012/09/a-feature-of-android-jelly-bean.html and Advantage of introducing Isolatedprocess tag within Services in JellyBean[Android]

How to create an Android service which can not be started programmatically?

Is it possible to create a service in Android, which can not be started programmatically via startService call? I mean, that an application with this service and the service itself should be started explicitly by a user only, and should be accessible only via bindService, in the case if the service is already started at the moment.
Is it possible to create a service in Android, which can not be started programmatically via startService call?
Not really. You could presumably call stopSelf() from onStartCommand(), though I have never tried this and it may cause unexpected problems.
the service itself should be started explicitly by a user only
There is no way for a user in Android to start a service directly, only by means of some app, such as your own app.
and should be accessible only via bindService
You have no means of preventing a call to startService(). If your service is not exported, no other application will be able to start or bind to it.
and if it's started, it's available to other applications via bindService
Only if you export it. As #Argyle notes, if you do not want other applications starting or binding to the service, do not export it.
Can't be started by startService? How else would it be started?
I suspect what you want is to ensure that only your own app can start it and not any external non-written-by-you apps. If that's the case, set
android:exported="false"
in your AndroidManifest.xml file for each Service you want protected in this way.

Autostarting Android service upon Application startup

Is there a possibility to autostart Service upon starting Application ? The problem is that I am developing separate UI component that depends on service. Ideally this service should be started as soon as hosted application starts. Could this be done via manifest only or could it be done at all ? I know I can start service from my UI component's code, but I want to start service immediatelly after starting main application even in case if my UI component hasn`t been created yet.
Thanks in advance.
Create a MyApp class which extends Application, and make sure it's declared in your manifest. MyApp's onCreate() is then a good place to start the service if you need to.
See the documentation for the Application class.
You want to use the PERMISSION that notifies you of boot completion. This will allow you to know that the device has started and take action, such as start your service.
RECEIVE_BOOT_COMPLETED Allows an application to receive the ACTION_BOOT_COMPLETED that is broadcast after the system finishes booting.

Avoid background processing in Android application

I am developing an application for a recognized financial institution. It is very important for them to manage security, and one of the requirements is that the application cannot run in the background. It was specifically requested that if a phone call is received, the application must get killed.
I tried using BroadcasterReceiver, by starting an activity when I hang up a call but apparently it runs as a service, and while my application is no longer running the activity is always started.
Is there any way to avoid background processing, like in iOS?
onPause() is called when your activity is going into the background.
If you overrode onPause() and stopped any sensitive activity in there, you would always catch the Activity going into the background.

Categories

Resources