I have an application with call activity, when I finish action in activity the activity is still running in background device. Now I want to stop the activity and not allow it to run in the background when it is finished. I'm also using killBackgroundProcesses but it's not working.
public void KillApplication(String killPackage) {
ActivityManager am =(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
android.os.Process.killProcess(runningProcess.pid);
am.killBackgroundProcesses(killPackage);
}
I just call finish(); and then I use the function KillApplication("mypackage");.
From the looks of it, your processes should be terminated. Are you thinking that the process is still running because the application still appears in the 'recents' list? If that's the case, take a look at the "Close application and remove from recent apps" question.
It explains how to remove an application from the recents list by using an Intent flag.
What do you mean by run in background?
The best practice is calling finish(), make sure all the process are stop, and let the OS fully kill the app when it need the resource.
But if you want to force kill, you can use
System.exit(1) instead of finish().
Related
Hi I am trying to make a chathead bubble, like the one facebook has, for an app in android studio. I have been able to successfully display the bubble using Overlay and make it a service which continues to run even after the app is closed (not killed). However when I open another app or if I dont use my phone for more than 10 minutes, the chathead bubble disappears, unlike Facebook's bubble. How can I go about making the bubble display on the home screen and other apps for a longer period of time(potentially forever)?
For context, I used https://www.androhub.com/android-floating-widget-like-facebook-messenger-chat-head/ to make the bubble, using a View and a Service.
Thanks in advance
Your app's service will get killed by the android system to save battery. There is no exact and easy way to do that. You have to implement multiple methods to do that.
Use Foreground Service(You may have already doing this but just in case if not.)
Ask for permission to restrict Battery Optimization.
In some devices like Xiaomi and Vivo, you need special permission to always run in the background ask for that.
Ask the user to lock the app in recent tab so it won't get killed by the system.
If you are implementing the service, override onStartCommand() and return START_STICKY as the result. It will tell the system that even if it will want to kill your service due to low memory, it should re-create it as soon as memory will be back to normal.
If you are not sure 1st approach will work - you'll have to use AlarmManager http://developer.android.com/reference/android/app/AlarmManager.html . That is a system service, which will execute actions when you'll tell, for example periodically. That will ensure that if your service will be terminated, or even the whole process will die(for example with force close) - it will be 100% restarted by AlarmManager.
I know that this call should not be made but at the moment is the only thing stopping the activity fast enough.
Basically after returning the app from the background (or using the DDMS "Terminate Application" button) some static variables are null. I would like to restart the activity in order to stop crashes and so all values are updated.
At the moment System.exit(0) has done what I want, but I know this is not good.
I have tried finish(), but that does not stop the activity fast enough, it keeps executing some instructions that still causes the crash.
Any suggestions on how to manage this problem.
Standard way to close your running android app is simple:
finish();
or more complicated way is this:
android.os.Process.killProcess(android.os.Process.myPid());
Only in Android api 21 > you can use >
getActivity().finishAndRemoveTask();
Finishes all activities in this task and removes it from the recent
tasks list.
It stops the application as fast as System.exit (0). But is it better than System.exit(0)? I don't know...
------------------ EDIT ---------------------
In below question
How to quit android application programmatically?
you can see all kinds of ways to stop your application.
According to the docs for Activity.finish():
Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
When you call System.exit(), you are actually terminating the JVM; by finishing the activity, you allow the Android JVM to do its cleanup.
Im developing a desktop app in java that when the app is closed by the frame I have an event call FrameClosing that make a function when user close the windows , the problem is when the app is close by the task manager or when the user turn off the windows and the app is running, i was trying this.
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
System.out.println("app was stoped");
}
});
but it's only works if app is closed by the Frame. I was reading about socket my app could can communicate with a website and if the communication between them I could detect that the program was closed but I dont really too much about it
I just want to detect when my app stop running
Your code is correct. If it is killed via the task manager though that is end of story. It is killed before it can run System.out.println. If that wasn't the case you would be able to do all sorts of stuff after the user said they want to kill it.
This answer is talking about C++ but the second point is the important part. If the program is killed via Task Manager or kill -9, there's really not much you can do about that. The program doesn't have access to that level and you'd have to have the program be looking for that specifically.
Rather than try to deal with that, have you considered designing the app so that an unexpected shutdown isn't a deal breaker? For example, doing intermediate logging while the program is running. You could use that to determine if the app was prematurely shutdown when its run next and do whatever is necessary before it starts working.
You could also look at this thread. Its talking about killing via the command line but it might help you in some situations.:
Logically, SIGHUP (terminal hangup) should be raised.
The equivalent of SIGHUP is provided through the callback you register with
SetConsoleCtrlHandler. Your callback function will be called on an
arbitrary threadpool thread with dwCtrlType = CTRL_CLOSE_EVENT. You've got
5 seconds to clean-up, you cannot cancel the close.
I have been doing my research, but I feel as if I am missing something.
I have an app with a login. Each time you open the app, you should be forced through that login page. You should never be able to resume onto any activity other than the login.
In the manifest I have
android:clearTaskOnLaunch="true"
on the main activity I wish to use as the login activity,
and
android:finishOnTaskLaunch="true"
as well as
android:excludeFromRecents="true"
on the rest of the activities.
The problamatic situation occurs when you go from the login to another activity, hit home, and relaunch the app via the icon. It should jump back to the login page, but it doesnt. Any idea?
I have also been installing as a regular apk, not via eclipse as I know that there is an issue with eclipse and some of the manifest attributes.
Perhaps if there is a way to detect that the activity launch came from the app icon press, I could manage it that way, but I dont think that is possible either.
In either onResume or onRestart you could check a series of flags, such as a login timeout, then force the user back to the login activity using an Intent, while at the same time finishing the original activity.
I like this method in favor or just finishing the app in either onPause or onStop because it gives you a chance to make some checks before blindly closing the application.
Or, you could try using the android:noHistory tag in your manifest file.
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
There are also other tags such as, android:finishOnTaskLaunch
Whether or not an existing instance of the activity should be shut down (finished) whenever the user again launches its task (chooses the task on the home screen) — "true" if it should be shut down, and "false" if not. The default value is "false".
More information here: http://developer.android.com/guide/topics/manifest/activity-element.html
The easiest and fastest way is probably to terminate the activity with finish(); in its onPause() since this is invoked when the App is put into the background.
It might be possible to solve with XML configuration as well, but not that I know of by heart.
I want to check whether the process is still alive or not through programmatically ,Can I do that I am trying to do it by process name in onCreate method but the issue is that the onCreate method is called always .When I check that in onCreate method I always get the process name and I can not kill the current app and switch to previous one.
Can anybody help me ?
Thanks in advance.
I'm not sure I understand why you need this. It sounds more like you are wanting to save your state between application changes (your app going to the background).
On the Android, there are no two programs running at the same time, basically (there are services, but those are different). Once your user goes back to the main screen, if they "launch" your application again, it will go to the first activity defined, unless you override some functions to restore the previous state of the application.
There is no "the previous one" to go back to.
Unless you have seriously messed around with your manifest, there will be only one copy of your application (in one process) at a time.
Refer to this article in the Android documentation, specifically the Processes and Threads section:
Application Fundamentals
When the first of an application's components needs to be run, Android starts a Linux process for it with a single thread of execution. By default, all components of the application run in that process and thread.