How to check if my application works in android (and not as a service)?

Problem: I have to check if my application is working or not (when the service is already running in the background). Based on this, I believe that I am either starting a specific activity or application.

The thing that I tried or came up with, but failed I tried to check the current current process and based on it tried to decide whether the application was working or not. The reason for the failure: to get the application launch status, as always, since I already had a service running in the background.

A possible solution, but not sure if this is possible in android 1. If we can start the service under a different name (package name) 2. Set some logical value to true and false to start and close the application, respectively. But is this a good approach?

EDIT ::

Sorry if I was not very descriptive before. I am trying to explain to you exactly what I am trying to achieve.

Whenever the user clicks on the notification. I have to execute either of two options after checking the launch of my application (excluding services, since they always work in the background). 1. If my application is already running, just start the required activity. 2. If the application is not running, I have to start it and then open the required activity.

For example, I receive a notification of receipt of a message. If my application is running, I must show the Inbox, otherwise I need to start the application and open the Inbox.

+4
source share
3 answers

Although the question is quite old, I would like to post my own answer, since I spent too much time finding a similar solution to the problem. My service works with the transfer of data collected by a visual application (camera, GPS, etc.). It must remain on when the user closes the application, because it needs to transfer data that has not yet been transferred to the server when the application was turned on. But he must switch his working mode to a slightly softer one (in order to save energy) (and he will finally close when the work is completed, but this is another story. First, I tried to send some command from the main activity to this service when he was about to close his user’s intentions, but that didn’t work if I just clicked on the application in the task list (the home button, long press or another on other phones). I found a different solution. I have a CommonData class for ordinary application objects. I noticed then if I write something like

 CommonData.runObject = new Object(); 

in the main onCreate action, this runObject parameter runObject set to null when the application is disconnected.

So, in my service, I just need to tell CommonData.runObject != null from CommonData.runObject == null to find out if my application is working or not.

+2
source

When your service runs in the background, your application is indeed running! If you want to check that any activity of your application is a background or background, I think that you can use the Activities lifecycle methods and set boolean in Application or you can get a list of running tasks in your service and check if any or your action:

 ActivityManager activityManager = (ActivityManager)Monitor.this.getSystemService (Context.ACTIVITY_SERVICE); List<RunningTaskInfo> activitys = activityManager.getRunningTasks(Integer.MAX_VALUE); isActivityRunning = false; for (int i = 0; i < activitys.size(); i++) { if (...) { isActivityRunning = true; break; } } 

But see.

Others:

Check if Activity is running from service

How to determine if one of my actions is in the foreground

Android activity detection

0
source

I’ll try to guess, since you didn’t understand what Hesan said, if you use the service, this application works, so it may be a different application than the service, when you say the application you can talk about activities, and it can be on the front in the background or in the background.

In any case, you can set the static logical value inside the set of services to false, and then set it to true or false if you think that your application will be launched based on any state of the application and your own definition as to whether the application is running . You need to redefine the state in which you want to include it and set the boolean value to true; and where you want to disable it, override and set it to false.

Look here to see the Activity life cycle, since you usually want to measure the life of applications through activity, as it is most noticeable to the user, however other components have similar life cycles, like the ones you can use

http://developer.android.com/training/basics/activity-lifecycle/index.html

0
source

Source: https://habr.com/ru/post/1443852/


All Articles