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..
Related
Is there any way to set the priority of android service? I want to run the service in background after destroying App's main actvity. So i am returning "START_REDELIVERY_INTENT" from my service. But restarting of service is taking some time (around 1-3 minutes). I want to restart the service instantly. Is there any way to set the priority of android service.
FYI, I am creating service using startService and passing an object through intent.putExtra().
Below snapshot is taken from setting/apps/running apps by my android device, which shows that app is restarting... My app is real time specific, so i want to restart it instantly...
You cannot control this behavior of Service as it depends on the OS and available resources. However, for a continuously running background service, I would suggest you to return START_STICKY - which tries its best to re-run the service once its destroyed due to any reasons.
The priority depends on the component type and the current app that the user is using. The highest priority have the current Activity that is displayed to the user and all foreground services. Foreground services must have a notification that cannot be dismissed. To create a foreground service you have to call startForeground(...) method from the Service itself passing the id of the notification and the Notification object itself. You can use the id if you want to update the notification later.
I recommend you to make the service bound and bind your activity to the service. This way the you can start and stop the service in your onResume and onPause methods.
The only way to increase service priority in this case is to make it a foreground service.
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
I am creating an application which sends the location information to the server. But when the back or home button is clicked the application is stopped. Is there a way to make the application run continuously in background and sends data to the server ?
Use a background Service. Services are application components which can run in the background without user interaction. A good tutorial on Service in android is from this.
You probably want to start the service on launch of your Activity, or you can register for system broadcasts like BOOT_COMPLETE to keep your service running from boot up.
However, keeping your service running throughout without user knowledge is a bad idea, and drains battery also. You probably want to wake up the service using AlarmManager, do processing, schedule next restart, and exit.
You need to run a Service class for your own application.
Refer docs for more information.
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.
I want to write a service for Android platform that is notified when the current foreground activity changes.
Basically the service should do some tasks only when the top activity changes.
Is there any way to subscribe and to be notified when this kind of event occurs ?
Or there is no possibility and the service should poll from time to time the list of running activities and to check what is the foreground activity ?Not preferable solution...
AFAIK there are two ways to do that.
Start a service and monitor the Activity Stack, you might check it here
Use an Accessibility Service, you could find a solution here
You should bind every activity to the service and you will know which activity is running.
try this:
List runningTaskInfos=actvityManager.getRunningTasks(1).get(i).topActivity .getPackageName();
this method will give info of activity's package name which is in foreground..............