I use following code to stop my app:
this.finishAffinity();
However after I finish my app I can see in the application manager my app still has stop button enabled.Does this mean the app is still running?Moreover the android studio thinks app is running too.
If I finish the app with this code:
this.finish();
System.exit(0);
Android studio treats app as finished, but app manager still shows stop button enabled.
I read calling System.exit is unpreferable way of finishing the app.
What is the correct one?
In short, don't worry about it.
You should run checks to ensure you aren't leaking a Context, but in general you don't need to worry about your App staying "active".
This simply means that it hasn't been fully terminated yet, because Android hasn't needed to do so. Until then, it keeps it partially alive to be able to bring it back quickly if the user wants it again.
This doesn't take any additional processing resources to keep it alive. Only some memory. When Android deems it necessary to claim resources, it will kill your App fully.
Just keep using finish();.
The Android Lifecycle:
If you don't want your app to be shown in the overview screen when it finishes, simply add the android:autoRemoveFromRecents="true" to your activity tag in the manifest. No need to use System.exit(0).
To Quit Application on Button click use this code :
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
Try it..
To kill the complete app and remove it from Runningapp list kill the app through its pid(its nasty)... use this lines before above code.
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
no need to use this.finish();
just finish(); is ok
app manager always show the app is running because killing a app may occur misbehavior to its activitys.
public void EXIT(View view)
{
finish();
}
android.os.Process.killProcess(android.os.Process.myPid());
Related
More or less the title. I call Application.quit() in Unity, and then in the onUnityPlayerQuitted method in my Android Studio app, I call this code
Intent intent = new Intent(this, Exit.class);
startActivity(intent);
However, this just closes the entire app. I've already got these statements in my androidmanifest.xml
android:launchMode="singleTask"
android:process=":UnityPlayerActivity"
so I can't quite figure out why my app closes instead of going to the Exit activity as I called for it to.
Solved this by changing Application.quit to Application.Unload in Unity, and then having the activities run on different processes using the aforementioned xml tags.
Originally, it received the BootComplete Action and tried to start automatically when the app completes booting. However, while checking because startActivity did not work, I found out that context.startActivity executed by the Action received from BoradcastReceiver does not work.
BroadcastReceiver
Intent startIntent = new Intent(context.getApplicationContext(),MainActivity.class);
startIntent.setFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(startIntent);
It's a very simple code that I can't explain, but it gets Received, but the app doesn't start. The existing apps seem to work without problems. It feels like a ghost.
There is a log like this, but I don't know what the problem is.
D/BootReceiver: BootRecived
D/ZLA: Setting app side flag to false due to ActivityStarter-Normal Launch
Please add the following permission to manifest and ask for user permission once when the app opened the first time by calling
startActivity(new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)); somewhere in your app :
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
if you are targeting/running on Android 10 then it isn't possible to startActivity, thats Android new policy (check out HERE)
now docs suggest to show Notification and user may pick it and this will start your Activity (or may remove it and your app won't start)
I would like to exit the app completely. I tried finish() and android.os.Process.killProcess(Process.myPid()) but they still leave app running in background.
A lot of people said that this is impossible, but my bank app is able to do that. When you exit that bank app, you can't see it in Recent Apps menu.
Does anyone know how to do it?
When you exit that bank app, you can't see it in Recent Apps menu.
That has nothing to do with a process. Processes and tasks are not particularly related.
Try finishAndRemoveTask() (API Level 21+), or perhaps finishAffinity() (API Level 16+), both methods on Activity. Or, as Thom Wiggers suggests in the comments, perhaps the app is not showing up in the recent-tasks list at all, via android:excludeFromRecents.
To remove activity from recents use this in the activity tag in manifest:
android:excludeFromRecents="true"
To remove application from background use below code. Please note that this will not remove the application from recents. It justs starts the application from the starting activity.
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
or
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
I have application A and application B.
I launch B with an intent from A in this way:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName("com.applicationB", "com.applicationB.MainActivity");
intent.putExtra(EXTRA_NAME,"name");
startActivity(intent);
When the user closes application A I want application B to close too. App B has JNI and uses exec() command.
I've tried android:sharedUserId but I got this error when app A tries to launch B with the code above:
Error running exec(). Command: [....] Working Directory: null Environment: (not null, it shows all the environment.
Any ideas?
you must use android IPC mechanisms like broadcast receiver
when application A closes it should send a broadcast and application B should register for a broadcast receiver to capture broadcasts from application A
see the documents for more info http://developer.android.com/reference/android/content/BroadcastReceiver.html
If both applications are programmed by yourself, you could consider to add a BroadcastReceiver in app B, that takes care, that all activities get closed. Before app A closes, call that receiver and it's done.
If you are not sure which activity is shown, you could extend the Activity class with a BroadcastReceiver, so that all activities get notified.
If app B is not programmed by you, you could use killbackgroundProcesses(), e.g.
ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses(myProcessId);
... however, this works only if app B is in fact in background. Additionally this is not the best option to close an app 'cause you do not know what the app is currently doing.
Edit: found my favorite example on that topic :-) check http://www.hrupin.com/2011/10/how-to-finish-all-activities-in-your-android-application-through-simple-call - it shows how to close all activities from within the app. However, it's not a big step doing this from another one ...
This code will run an app automatically after booting the system, but the app will close after pressing the back button.
If the app is run normally by clicking it's icon. It will continuously run even after pressing the back button or running other apps.
public class AutoBoot extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent(context, MyActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
My question is, how to make this auto run code to continuously run even after pressing the back button or running other apps?
You can probably start a Service here if you want your Application to run in Background. This is what Service in Android are used for - running in background and doing longtime operations.
UDPATE
You can use START_STICKY to make your Service running continuously.
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
As apps run in the background anyway, I'm assuming what your really asking is how do you make apps do stuff in the background. The solution below will make your app do stuff in the background after opening the app and after the system has rebooted.
Below, I've added a link to a fully working example (in the form of an Android Studio Project).
This subject seems to be out of the scope of the Android docs, and there doesn't seem to be any one comprehensive doc on this. The information is spread across a few docs.
The following docs tell you indirectly how to do this:
https://developer.android.com/reference/android/app/Service.html
https://developer.android.com/reference/android/content/BroadcastReceiver.html
https://developer.android.com/guide/components/bound-services.html
In the interests of getting your usage requirements correct, the important part of this above doc to read carefully is: #Binder, #Messenger and the components link below:
https://developer.android.com/guide/components/aidl.html
Here is the link to a fully working example (in Android Studio format):
https://developersfound.com/BackgroundServiceDemo.zip
This project will start an Activity which binds to a service; implementing the AIDL.
This project is also useful to re-factor for the purpose of IPC across different apps.
This project is also developed to start automatically when Android restarts (provided the app has been run at least one after installation and app is not installed on SD card).
When this app/project runs after reboot, it dynamically uses a transparent view to make it look like no app has started but the service of the associated app starts cleanly.
This code is written in such a way that it's very easy to tweak to simulate a scheduled service.
This project is developed in accordance to the above docs, and is subsequently a clean solution.
There is, however, a part of this project which is not clean: I have not found a way to start a service on reboot without using an Activity. If anyone reading this post has a clean way to do this, please post a comment.
Starting an Activity is not the right approach for this behavior. Instead have your BroadcastReceiver use an intent to start a Service which can continue to run as long as possible. (See http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle)
See also Persistent service