Autostarting Android service upon Application startup - android

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.

Related

How run other process to background?

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

How to detect user activity/inactivity in Android service/background application?

How to detect user activity/inactivity in android service/background application - from same or other applications?
I want to know that user stop touching screen and not busy with another activity to show notifiation popup. Not sure is there no other method than use AccessibilityService.
One of the ways is to know that from reading the Android logs
Use a boot receiver to setup the AlarmManager (and of course also check to start the polling from your main Activity too, for the case when your app is installed and the system is not booted) and have the AlarmManager send an Intent for another receiver.
I think they answered it in this question to do what you are looking for:
android: running a background task using AlarmManager
Define a interface in activity and service.
in oncreate of activity set reference to activity in service method.
Now you have activity reference for entire service lifetime.
another way to do this is, application class, same application class is shared by activity and service, so you can both of these from each other using application class.
If you want see from applications then use broadcastreceivers that is the only wau

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.

How to register a Service that will run all the time

I would like my android app service to run all the time.
that is -
1. right after installation,
2. on boot
3. if its closed - it will be relaunched -
how do i achieve all of the above code-wise?
thanks!
I am not putting the code here however you can easily find it.
Right after installation use the default activity to launch the service, in case you do not have any UI then create an activity without any UI (no setContentView) and in its onCreate start the service.
You need to create a broadcastReceived that listens to ACTION_BOOT_COMPLETED and call that as Service Manager. On Receiving the broadcast in that receiver just start the service again.
Make your service as foreground and that should ideally take care of this scenario.
You shouldn't use the Service foreground feature! The best practice in current android version is to return START_STICKY from your Service's onStartCommand(). It will cause the Android system to re-launch your service.
Regards.

Notify when application comes to foreground

I have a service which has to monitor which application is in the foreground and based on the application do some functionality.
I can get the list of foreground applications, using ActivityManager.getRunningAppProcesses().
But with this I cannot monitor when the foreground application changes.
ex. when Application 1 which was foreground goes to background and a new Application 2 comes to foreground, the service should be notified about this.
Are there any Broadcasting happening in Android, so that my service can register to that and when there is a change in foreground application, the service gets a notification and can do the functionality as required.
I do not want to do any changes in the Applications...
Any possibilities please suggest...
Then a simple solution is Create static variable of type Context() in some Class and in all onResume() methods activities initialize it with their Instances.. and check it in service using if conditon and carry on..

Categories

Resources