如何检查,如果我的应用程序在Android操作系统(而不是作为服务)?

问题描述:

问题:
我要检查,如果我的应用程序运行与否(当一个服务在后台已运行)。在此基础上,我想要么启动特别的活动或应用。

Problem: I have to check if my app is running or not(when a service is already running in background). Based on it I am suppose to either start particular activity or the app.

的事情我想还是想出但未能
我试图查看当前运行的进程,并在此基础上尝试,如果应用程序正在运行与否来决定。
失败原因:获取应用程序的运行状态一如既往正确的,因为我已经服务于后台运行

Thing I tried or come up with but failed I tried to check current running process and based on it tried to decide if app is running or not. Reason for failure: Get app running status as always true since I already had service running in background.

可能的解决方案,但不知道这有可能在Android的
1.如果我们可以运行不同的名称服务(包名)
2.分别设置一些布尔值true和false在应用程序启动和关闭。但是,这是一个很好的方法?

Possible solution but not sure if that possible in android 1. If we can run service by different name(package name) 2. Set some boolean value to true and false on app start and close respectively. But is this a good approach?

编辑:

很抱歉,如果我是不是很描述更早。我试图解释,你究竟是什么我试图acheive。

Sorry if I wasn't very descriptive earlier. I try to explain you what exactly I'm trying to acheive.

在通知每当用户点击。我如果我的应用程序启动(不包括服务,因为他们总是在后台运行)检查后做两个选项的任何一出。
1.如果我的应用程序已经推出仅启动所需的活动。
2.如果应用程序无法启动,然后我要启动它,然后打开所需的活动。

Whenever user click on notification. I have to do any one out of two option after checking if my app is launched(excluding services since they are always running in the background). 1. If my app is already launched just start the required activity. 2. If app is not launched then I have to launch it and then open the required activity.

例如
我得到一个消息到达通知。如果我的应用程序启动我必须表明的收件箱,否则我要启动我的应用程序,然后打开收件箱。

e.g I get a message arrival notification. If my application is launched I have to show inbox otherwise I have to launch my application and then open the inbox.

虽然这个问题是相当老了,我想后我自己的答案,因为我花了太多时间寻找类似的问题的解决方案。我的服务正在传送由可视化应用程序(照相机,GPS等)收集的数据。它具有留在当用户关闭应用,因为它需要发送尚未已经传输到服务器时应用是在数据。但它有切换的工作模式,以多一点的软(以节省电源)(和它最终将自行关闭,当工作结束时,但那是另一回事。首先,我试图从主要活动发送一些命令,这个服务当它即将关闭用户的意图。但是,如果我只是轻弹我的应用程序关闭任务列表(home键长preSS,或其他一些其他电话)并没有工作。
我找到了另一种解决办法。我有一个应用程序的通用对象 CommonData ​​ code>类。我注意到,如果我写的东西像

Though the question is rather old, I would like to post my own answer, as I spent too much time seeking for the similar problem's solution. My service is working transmitting data gathered by visual application (camera, GPS, etc.). It has to stay on when user closes application, as it needs to transmit data that hasn't been already transmitted to the server when app was on. But it has to switch its working mode to a bit more soft (to save power) (and it will finally close itself when the job is finished, but that's another story. First, I tried to send some command from main activity to this service when it was about to close on user's intent. But this didn't work if I just flicked my app off in the task list (home button long press, or some other on other phones). I found another solution. I have CommonData class for app's common objects. I noticed that if I write something like

CommonData.runObject = new Object();

在主要活动中的的onCreate ,这个 runObject 设置为空当应用程序被弹开。

in main activity's onCreate, this runObject is set to null when app is flicked off.

所以,在我服务,我只需要告诉 CommonData.runObject!= NULL CommonData.runObject == NULL 来看看我的应用程序的运行,还是不行。

So, in my service I just need to tell CommonData.runObject != null from CommonData.runObject == null to see if my app's running, or not.