Android application exit application

Finding the reason for the exit in Android Application

When working with an Android application, we might have to keep an eye on crashes or unwanted ways that the user exits the app. We use many third-party libraries to log exits in android.

Now, with Android R, we can log the exits in our app which can help us fix any issue that might exist in our app.

In this blog, we are going to talk, how we can log the exits of the user from our application.

Let’ s first discuss the type of crashes and exits we might see in our application. There can be a crash for an exception or maybe there might be an ANR that might lead your app to crash and drop users out of the application. The users might also exit the app intentionally when they have completed using the app.

Consider an example, let us say we are using WhatsApp and we want to log all the actions when a user exits the app. It can be a crash or an exception, or maybe a user action to close the app.

So, to get all the information we are going to use the ActivityManager .

But before that,

What is ActivityManager?

ActivityManager gives information about and interacts with, activities, services, and the containing process. Methods in this class give us certain information that we can use to debug our application.

So, first, I will create a project with MainActivity like,

Now, we will initialize the ActivityManager like,

Now, we will use ActivityManager to get the list of historic exit reasons for the app. To get the list, we will use,

Here, getHistoricalProcessExitReasons takes three parameters,

  • Package name: Here, we passed the package name of our application
  • PID: We passed 0 as default to get all the reasons
  • Size: The size of the list of exit reasons. So, we will get a list of type ApplicationExitInfo with only one item.

Here the exitList returns the history of exit, not the exit information in real-time as it provides the historical data.

Now, let’s first set up the exitList to return us the cause and reason of our crashes.

In our onCreate(), I will add,

Since we have only one item in the list lastExitInformation will get the value from the list using .first()

Now, we will Log the exit reasons, time and description using,

  • lastExitInformation will return an integer which corresponds to the reason of the crash
  • Timestamp returns Time in Epoch and,
  • The description returns the descriptive version of the crash.

Now, let’s add a TextView in XML file like,

and set the reason of crash to the textView using,

We are done setting up everything to get the historic exit reasons. Now, let’s start logging the reasons.

Case 1:

When we close the app from the recent app’s tray and then reopen the app again we would see the following log,

  • Reason code is 10 which means, REASON_USER_REQUESTED.
  • We got timestamp as well in our log and,
  • We got the description as remove a task, as we removed the app from the recent app’s tray.

We can also see the reason code in the textView .

The reason code and its meaning are,

So, for example, if we get a crash the reason code would be 4 according to the above table.

Читайте также:  Mortal kombat new era 2021 android

Case 2:

When we get a crash in our application. So, first, let’s introduce a crash in our application. We will create a button using,

and on click of textView, we will invoke the button’s click listener like,

This would lead to a crash of our application because the button is not mapped to any widget in XML.

When we reopen the app again, the app will generate a log,

Here, the reason code is 4, which maps to REASON_CRASH from the above table and the description also specifies crash.

If any exception occurs as well, the reason code would be REASON_CRASH

Case 3:

Let us terminate the JVM using System.exit().

So, In on the click of textView, we will add System.exit() like,

This will terminate the app. When we reopen the app, it will generate a log like,

Here, we won’t see any description but the reason code is 1 which maps to REASON_EXIT_SELF. This means it terminated itself.

Case 4:

When our application gets ANR.

So, to generate an ANR we will run an infinite loop on click of textView using,

So, when App gets an ANR the Log generated would be,

Here, the reason code is 6, which means REASON_ANR.

These are a few cases where we can log our exits form our application.

In this blog, we are using the maximum size as 1. If we change it to any number more then 1, let’s say maybe 10. It will return the list of last 10 exit reasons information. To set max size to 10, we update the code,

This is how we can use getHistoricalProcessExitReasons to get the reasons for the exit of the app which might be very useful for developers to understand the user’s behavior of using the app or maybe checking for crashes.

Источник

Exit from an Android application

Normally, Android applications do not need an “Exit” button because they have a different lifecycle than, let’s say, a Windows application. To exit an application in Android, the user needs to press the home button and that’s it, but the application is not really closed, it’s sent to the background (the user can no longer see it) and the Android system will manage it’s state from there on, closing it only if the system needs more resources. But what if you really want to provide an “Exit” function to the user ? This article presents some of the choices you have available.

As I said earlier, you don’t usually need an exit function for your Android application, but you could add one if you really want to. The reasons for doing something like this are various and I can tell at least one of them : you want to clear some sensitive data when the user “exits” the application.

Close current activity
If you’re developing a really simple application (can’t really see why you would need an exit function in this case) you can exit the application with just a single line of code added to your exit button listener :

This closes the current activity, witch in some cases might be enough. Beware that if you call this from a child activity, the parent will still be visible. The same outcome is reached if you call

In other words this is pretty much useless for a real life scenario as you would have to provide your user with the possibility to exit from any part of the application instead of forcing him to navigate backwards to the root activity and then exit.

Close application and clear stack
This applies to more complex applications and could be used to clear “sensitive” data. The main idea behind this solution is that a child will call an exit method but this method will NOT call finish() on the child itself, instead it will try to start the main activity setting appropriate flags to clear the activity stack, and then it will send a message to the main activity telling it that it needs to exit, when the main activity receives this message , it will do any necessary cleanup and will then exit (this being the first activity in the stack it’s enough to call “finish()”).
I think it sounds more complicated than it really is so let’s look at some code. In your child activity you would have a button (or a menu item or whatever you want), that will allow the user to exit. In the click listener for the button you would add something like this :

Читайте также:  Виды оболочек для андроид

// start main activity Intent exitIntent = new Intent(this,IMDMainActivity.class); exitIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP|Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(exitIntent); IMDMainActivity.exitHandler.sendEmptyMessage(0);

So, what you need to do is :

  • create an intent for your main activity;
  • set flags to clear the activity stack;
  • start the main activity intent;
  • send a message to main activity.

Sending messages between activities is done with a handler.

In our main activity, we declare our handler, that will receive messages from all child activities :

Because we don’t send any other types of messages, it’s ok for the child activities to send an empty message and pass 0 as a parameter (remember the code from the child activity).
That was it, your application will have a decent exit functionality.
Note : Your main activity will start with the flags to clear the activity stack only when the user wants to exit the application explicitly (clicks an Exit button). In any other cases, for example the user receives a phone call when using the application, the application will behave as any other Android application : it will send the current activity to the background (keeping the activity stack) and will resume it when possible (after the call has ended for example).

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

Источник

Android Keep Broadcast Receiver Running After Application Exit

In some cases, you may need to create a broadcast receiver that can still run in the background after the android app exit. This example will just tell you how to do that use a broadcast receiver and android service object.

1. Screen On Off Broadcast Receiver.

  1. Below java files are the example project used files.
  2. First, we will create a broadcast receiver that can listen and process the android screen on/off broadcast event as below.
  3. As you can see, we will check the broadcast action value and log debug message based on that value in the broadcast receiver’s onReceive() method.
  4. ScreenOnOffReceiver.java

2. Register And Unregister ScreenOnOffReceiver In Activity.

  1. Now we will create an activity and register the ScreenOnOffReceiver object in it’s onCreate() method, and unregister the receiver in it’s onDestroy() method as below.
  2. ScreenOnOffActivity.java
  3. Below is the layout XML file for the above activity.
  4. activity_screen_on_off.xml
  5. Run the above activity in the below steps.
  6. Start the activity, there is a log message which said the broadcast receiver has been registered in the activity’s onCreate() method.
  7. Press the power button to turn off the screen.
  8. Press the power button again to turn on the screen.
  9. You can see log data in the android monitor console for the above steps.
  10. Type the back menu to exit the activity. You can see the broadcast receiver is unregistered in the activity’s onDestroy() method also.
  11. Press the power button to execute steps 2, 3 again, but there is not any log data printed in the android monitor console.
Читайте также:  Смартфон 7 дюймов экран андроид

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/BiqbgHa3ciQ

3. Register And Unregister Broadcast Receiver In Android Background Service.

  1. When you register the broadcast receiver in the android activity class, it will be stopped after the activity exit.
  2. To resolve this problem, we will create an android service object, and register and unregister the broadcast receiver in the service object.
  3. Because the android service object will still run in the background after the activity exit, so the broadcast receiver will still run also after the android app exit.

3.1 Create Android Service Class.

3.1.1 Create A Java Class That Extends android.app.Service.
  1. ScreenOnOffBackgroundService.java
3.1.2 Add Service Xml Tag In AndroidManifest.xml File.
  1. AndroidManifest.xml
3.1.3 Change Activity Java Code To Below.
  1. Please notice the java code that starts the service object.
  2. ScreenOnOffActivity.java
  3. Run the example again, you can see the below picture. From the android Logcat output, we can see the broadcast receiver still running after the android app exit.

Источник

Android Keep Broadcast Receiver Running After Application Exit

In some cases, you may need to create a broadcast receiver that can still run in the background after the android app exit. This example will just tell you how to do that use a broadcast receiver and android service object.

1. Screen On Off Broadcast Receiver.

  1. Below java files are the example project used files.
  2. First, we will create a broadcast receiver that can listen and process the android screen on/off broadcast event as below.
  3. As you can see, we will check the broadcast action value and log debug message based on that value in the broadcast receiver’s onReceive() method.
  4. ScreenOnOffReceiver.java

2. Register And Unregister ScreenOnOffReceiver In Activity.

  1. Now we will create an activity and register the ScreenOnOffReceiver object in it’s onCreate() method, and unregister the receiver in it’s onDestroy() method as below.
  2. ScreenOnOffActivity.java
  3. Below is the layout XML file for the above activity.
  4. activity_screen_on_off.xml
  5. Run the above activity in the below steps.
  6. Start the activity, there is a log message which said the broadcast receiver has been registered in the activity’s onCreate() method.
  7. Press the power button to turn off the screen.
  8. Press the power button again to turn on the screen.
  9. You can see log data in the android monitor console for the above steps.
  10. Type the back menu to exit the activity. You can see the broadcast receiver is unregistered in the activity’s onDestroy() method also.
  11. Press the power button to execute steps 2, 3 again, but there is not any log data printed in the android monitor console.

If you can not watch the above video, you can see it on the youtube URL https://youtu.be/BiqbgHa3ciQ

3. Register And Unregister Broadcast Receiver In Android Background Service.

  1. When you register the broadcast receiver in the android activity class, it will be stopped after the activity exit.
  2. To resolve this problem, we will create an android service object, and register and unregister the broadcast receiver in the service object.
  3. Because the android service object will still run in the background after the activity exit, so the broadcast receiver will still run also after the android app exit.

3.1 Create Android Service Class.

3.1.1 Create A Java Class That Extends android.app.Service.
  1. ScreenOnOffBackgroundService.java
3.1.2 Add Service Xml Tag In AndroidManifest.xml File.
  1. AndroidManifest.xml
3.1.3 Change Activity Java Code To Below.
  1. Please notice the java code that starts the service object.
  2. ScreenOnOffActivity.java
  3. Run the example again, you can see the below picture. From the android Logcat output, we can see the broadcast receiver still running after the android app exit.

Источник

Оцените статью