I'm having requirement where i need to check whether application is running in background or killed. I'm doing like as shown below.
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> alltasks = am.getRunningTasks(1);
for (ActivityManager.RunningTaskInfo aTask : alltasks) {
// Used to check for CALL screen
if (aTask.topActivity.getClassName().equals("com.android.phone.InCallScreen")
|| aTask.topActivity.getClassName().equals("com.android.contacts.DialtactsActivity")) {
// When user on call screen show a alert message
Toast.makeText(context, "Phone Call Screen.", Toast.LENGTH_LONG).show();
}
// Used to check for SMS screen
if (aTask.topActivity.getClassName().equals("com.android.mms.ui.ConversationList")
|| aTask.topActivity.getClassName().equals("com.android.mms.ui.ComposeMessageActivity")) {
// When user on Send SMS screen show a alert message
Toast.makeText(context, "Send SMS Screen.", Toast.LENGTH_LONG).show();
}
// Used to check for CURRENT example main screen
String packageName = "com.sap.rex.ui";
if (aTask.topActivity.getPackageName().equals(packageName)) {
isInBackground = false;
Toast.makeText(context, "Current Example Screen.", Toast.LENGTH_LONG).show();
}
The above code is only checking whether the top activity package name and telling whether it is in background or not.
If we open two or three application on top of it, since it only take top application package name it is saying it is not in background even though it is there.
Please let me know where i'm doing wrong.
Thanks