Get the status of one application from another application - android

I have two applications such as Activity and Service. I start Service from Activity. When I exit the activity it stops but it is not completely getting stopped, running in background(Could see in Settings->ManageApplications menu).
So I used android.os.Process.killProcess(android.os.Process.myPid()) in onDestroy() function which completely terminates application. But the issue is that it also stops my service since it was started from my activity.i also tried using the below lines,
ActivityManager activityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(this.getApplication().getPackageName());
But still it stops my service since it will stop the associate processes as said in document.My question is that how can i terminate my activity completely without killing my service. Thanx in advance.
Scenario
Assume there are two applications 'a' & 'b'. I stop Application 'a' using finish() call, 'a' closes successfully. Now I check the status of 'a' from 'b' using runningProcessInfo call and i still find that application 'a' is running in the background.

My question is that how can i terminate my activity completely without killing my service.
Call finish(), which happens automatically when the user presses BACK (by default).
When i exit the activity it stops but it is not completely getting stopped, running in background(Could see in Settings->ManageApplications menu)
An application is not an activity. An application is a collection of components. In your case, your application consists of at least one activity and at least one service, based upon the information in your question.
So i used "android.os.Process.killProcess(android.os.Process.myPid())" in onDestroy() function which completely terminates application.
Do not do this.

Related

How an Activity is killed by Android?

As we know Android would kill a paused Activity in some conditions. And there is a FIFO back stack of Activities.
My questions are as follows:
How Android kills an Activity without pulling it from the back stack (it might affect the top active Activity)
After killing it, what things are released from this Activity? And can I still get this Activity's instance?
Android does not kill Activities "separately", it kills the whole app process with all Activities.
The only way to get an Activity killed by the system is to set Don't keep Activities flag in device's Developer Options. However this option is just for development, not for applications in release.
Activity can't be killed but Os can kill the whole application. In this case you can try finish()/finishActivity()/context.finish() to finish the activity. While you finish an activity backpress will not return to the previous activity.
System can't kill activity, it can call the whole app.
And when it kills the whole app, it doesn't call any method from activity (onStop(), onDestroy(), etc)
Finally, you can't get activity instance.

Stop IntentService when user goes back to home screen in multi-activity app

My Android application exists of a main activity in which the user has the ability to start several other activities. In order to have a permanently working keyword recognition (speech recognition), I use an IntentServicethat is started in the onCreate() of the main activity. The service needs to be working as long as any activity of the app is in the foreground.
However, this service uses the microphone all the time, so it's desirable to stop the service once the user returns to his/her home screen. I know the basics about activity lifecycles, but my question is: what is the best way to stop the IntentService when the user returns to the home screen from any activity?
To restart the service when the app is re-opened, I was thinking of a superclass that all activities inherit from. In the superclass I would set a static boolean serviceStopped that is set to true when the user goes to his/her home screen (and the service is stopped). In the onResume() method, this boolean will be checked and the service will be restarted if needed.
As I know there is no way to detect if the user leave your application and is in the home screen.
I think there is no a simple way to detect if your application is open/closed; however I found this How to get the list of running applications? and I think it could help you.
You could also try to start a Service in the background and check when any of your activities start or is destroyed.

My Android APP's process has been killed, but still in RunningTask

I have got some programs, need help.
I switch my app(have activity) to background(eg:By Home-Key), and use other APP to kill it.
then check it by Using getRunningTasks() and getRunningAppProcesses(), but i can not understand the result : I can find the app's Activity(TopActivty/BaseActivity)in the RunningTasks, but not in RunningAppProcesses
My problems:
1、Home can i remove all Activity when the process been killed?
2、In this case, how can i restart my APP when click the app-icon?
Thanks
can i remove all Activity when the process been killed?
When your process goes away, your activities, services, threads, and all that go away as well.
However, the "task" remains. This is basically a description of the back stack for your app, and any apps launched from it that were launched into your task versus starting a fresh back stack in another task. This description includes things like your saved instance state (i.e., onSaveInstanceState() Bundle), the Intent that was used to start the activity, etc.
how can i restart my APP when click the app-icon?
If the user is returning to your app via the recent-tasks list, ideally you do not "restart" the app, but return the user to where the user left off.
The developer documentation covers various ways that you can control, somewhat, the behavior of the back stack and your app (or individual activities) as they come back into being when the user returns to one of your tasks.

Service onDestroy() called when activity started from another one

Firstly, sorry for my English if it's not enough good. I'm having some problems in my application.
Starting, my app has multiple activities and one service which works in background since the first activity execute it. If I press back button on my root activity, I exit from the app but the service continue working. Then, I go into the app back, and the service work perfectly. My problem comes when I press a button to exit the application (there, I stop service and finish the root activity mainly) and then exit without any problem, when I want to enter the app again, the service is started, but if I want to change to another activity (which doesn't have the serviceConnection) my service get called onDestroy() method without any reason for that. I don't have how to continue, because the usual way to execute in this case is the service go on working as the first case.
Thanks a lot.
There is for sure a reason why onDestroy gets called.
In the first section of 'Services' in the developer guide, you can read the following:
Multiple components can bind to the service at once, but when all of
them unbind, the service is destroyed. (link)
So, if all components unbind from the service, the service will get destroyed. When you enter the activity that is not bound to the service, the service will be destroyed.
I'm wondering why you don't want your service to be destroyed since you don't need it in your 'another' activity?

How to kill app once focus is lost?

I'm trying to get the main activity for my app to finish (not remain an active application in background) while keeping the process alive for a service.
If the user presses the home button and leaves my app or another activity is launched from outside of my app, I want the activity to be closed and NOT listed as an active application.
It is important, however, that the process stays alive. I don't want my background service to die as well.
What's the best way to go about doing this?
You should not forcibly close the application as the system does well in handling this itself. Instead you should call finish() to signal that the app is done and can be disposed of(your service will continue running).
By default Services don't have a UI. Once started they run until they crash or are killed . The user can close your app and launch a new one and the Service will persist.
Activities on the other hand are only running when they are visible. When the user navigates to another activity the previous activity is paused, stopped, or killed.
A simple way to do what you've briefly described above would be to create an Activity that starts a Service. That way when use navigates away form your Activity the Service will keep running in the background.
If you want your activity to die completely whenever a new Activity comes into view simply put a call to finish() in the onPause() or onStop() methods for your activity (which ever is more appropriate for your app).

Categories

Resources