i want to check whether the my application is in a background or in a Foreground and i also want to check the app in open or not using BroadcastReceiver
public class CheckRunningApplicationReceiver extends BroadcastReceiver {
Context mContext;
public int mId = 1000;
NotificationManager mNotificationManager;
@Override
public void onReceive(Context aContext, Intent anIntent) {
mContext = aContext;
Boolean isAppOpen = isApplicationSentToBackground(aContext);
mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (isAppOpen) {
//openNotification();
} else {
//mNotificationManager.cancel(mId);
}
}
private void openNotification() {// Instantiate notification with icon and
// ticker message
Notification notification = new Notification(R.drawable.ic_launcher,"Notification message!", System.currentTimeMillis());
PendingIntent i = PendingIntent.getActivity(mContext, 0, new Intent(mContext,MainActivity.class), 0);
notification.setLatestEventInfo(mContext, "Notification Created","Click here to see the message", i);
notification.flags = Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(mId, notification);
}
public static boolean isApplicationSentToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
}
is there any solution for this, help me , Thanks.