Je veux vérifier si mon application est en arrière-plan ou en premier plan et je veux également vérifier si l'application est ouverte ou non en utilisant un 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,"Message de notification!", System.currentTimeMillis());
PendingIntent i = PendingIntent.getActivity(mContext, 0, new Intent(mContext,MainActivity.class), 0);
notification.setLatestEventInfo(mContext, "Notification créée","Cliquez ici pour voir le 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 tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
}
Y a-t-il une solution pour cela, aidez moi, Merci.