Reputation: 2687
I have an application in which I have a particular event receiver. My goal is to display a dialog box when the application is running, otherwise (ie if the application is not running) show a notification.
The events such as notifications or displaying dialog boxes I have implemented. I can not determine whether the user has the application on or off.
Any ideas ?
Upvotes: 1
Views: 73
Reputation: 3875
public boolean isAppRunning()
{
boolean appFound = false;
final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo recentTask : recentTasks)
{
if (recentTask.baseActivity.getPackageName().equals("package.Name"))
{
appFound = true;
break;
}
}
return appFound;
}
need permission also
<uses-permission android:name="android.permission.GET_TASKS" />
Please check one my question is related to your question. in that please check comments.
Check application is minimized/background
Upvotes: 1
Reputation: 23354
Exact here -
ActivityManager activityManager =(ActivityManager)gpsService.this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningServiceInfo> serviceList= activityManager.getRunningServices(Integer.MAX_VALUE);
if((serviceList.size() > 0)) {
boolean found = false;
for(int i = 0; i < serviceList.size(); i++) {
RunningServiceInfo serviceInfo = serviceList.get(i);
ComponentName serviceName = serviceInfo.service;
if(serviceName.getClassName().equals("Packagename.ActivityOrServiceName")) {
//Your service or activity is running
found = true;
break;
}
}
if(found) {
//Close your app or service
}
}
Source - how-can-i-check-if-my-app-is-running.
Upvotes: 1
Reputation: 4673
ActivityManager am = (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfoList = am.getRunningTasks(1);
ComponentName componentName = runningTaskInfoList.get(0).topActivity;
String runningActivityName = componentName.getClassName();
String runningActivityPackageName = componentName.getPackageName();
It requires "android.permission.GET_TASKS"
permission.
Upvotes: 1