how to detect app launching event in android and start broadcast reciever - android

I have to make a service which run in background with no icon of app and when any installed application is launched then my app detects and execute some functionality.
How can I achieve that?
Thanks in advance.

There is no broadcast to know when an app is launched. You can have a service running which has to periodically check the currently running tasks list to see if a particular app has been launched.
You definitely can make the service run with no notification icon, but that will simply increase the probability of your service getting killed.
You can learn about services from here : http://developer.android.com/reference/android/app/Service.html
You can get the list of running tasks from getRunningTasks() method of the ActivityManager.

Related

Android implementation service and launch in background also if the application is died

I am trying to find how to enable the application to perform a background task.
I use the example seen here "http://saigeethamn.blogspot.fr/2009/09/android-developer-tutorial-part-9.html" and I do not know if that is how I have proceed to execute a script permanently?
Also how to get it reactivated when starting the mobile?
So how should we do and if we can so that the service works even if you force stopping the application?
A big thank you for your help
No service can work if the user force stops an application. Force stopping puts the entire app into a state where no services, receivers, or activities can be run unless the user explicitly does so.
If you want to start a service when the phone starts, you need a broadcast receiver for the BOOT_COMPLETE broadcast.

What happens to background service when the app is updated in android?

Let's say I started a repeating background service that was stated on first app launch and on boot. What happens when I provide an update of the app. Will that background service be killed?
Will user have to open the app again to register the background service again or app will get some callback on update?
Edit-1: As one of the answer suggest if app has to be relaunched again to start the service then how does alarm application works fine after the update without relaunching(I believe it usages background service to start the alarm)?
Will that background service be killed?
It will be killed.
Will user have to open the app again to register the background service again or app will get some callback on update?
It depends. Basically it'd require user activity as app is not relaunched automatically after update. But if you target API 12 or higher (which you should nowadays) you can try to use ACTION_MY_PACKAGE_REPLACED broadcast. As per doc:
Broadcast Action: A new version of your application has been installed over an existing one. This is only sent to the application
that was replaced. It does not contain any additional data; to receive
it, just use an intent filter for this action.
so you can do you stuff either in BroadcastReceiver trigger something once you receive this broadcast.
The service will be killed and needs to started again.
A Service doesnt run on a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
So when the application is updated, the application is sent to the stopped state.
You can test this.
From google play store initiate a update for the app (which has a service E.g Whatsapp).
Open the app and wait for it to complete. It stops. you can check the internal running processes. Connect the phone to DDMS. Check the processes.

Continuously running an app

I am making an Android app which is supposed to monitor the other running apps.
The question is, how do I make my app run continuously from when i first activate it. It should also start running by itself when I switch off and reboot the phone.
Any suggestions on how to ensure this? I am considering using a background thread but I am not convinced this alone will suffice.
You should make an android service that running continuusly in background
http://developer.android.com/guide/components/services.html
http://www.vogella.com/tutorials/AndroidServices/article.html
http://www.tutorialspoint.com/android/android_services.htm
Well you should try to have an Service into your app that could keep track of the other App's running
So Every Time the App start's the Service Would Start And Accordingly give The Updated to your app regarding the Other App Working
For Boot Time Start you Should Try System BroadCast Receiver
You should Really Follow this Tutorial
Create a service to monitor apps in the background.
http://developer.android.com/guide/components/services.html
To make the app load at boot:
Create a BroadcastReceiver and register it to receive ACTION_BOOT_COMPLETED. You also need RECEIVE_BOOT_COMPLETED permission.
Refer to this:
Android -Starting Service at Boot Time
You have to use a Service. But keep in mind that your service can be killed and restarted after a while.
And you should register a broadcast receiver to listen for ACTION_BOOT_COMPLETED so that you can start the service once the phone booted successfully.

Android, running an app in the background

I have an app which consists of a simple GUI and a few Broadcast receivers.
I found somewhere on here how to start an app on boot of the device but cant seem to locate it atm.
However i need more i need it to start in the background and just listen using the broadcast receivers and then return to this state after the Gui(the visible part of the app) has been used.
Is there a way of doing this?
Thanks.
Look into the Service class. Services run in the background and have no direct user interaction.
http://developer.android.com/reference/android/app/Service.html

Registering my broadcast receiver to run when an app is launched?

I want to run some code when an app is launched, so my broadcast receiver has to be notified when user open any app.
Is there any way to do it?
No, sorry, there are no Intents broadcast when an application is started, nor when an activity is started.
Probably you can have some trigger from a method like onCreate etc which would be the first method to be hit whenever your app is launched.
This is possible using accessibility services. An event AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED is recieved when the foreground activity is changed. The package name of the app in the foreground can be obtained from within the callback.
I have provided the code snippet in the answer to a similar question here.
How to monitoring app swaping in foreground?
The complete code for sample app to get the foreground process name is also available at https://github.com/abinpaul1/Android-Snippets/tree/master/GetForegroundService

Categories

Resources