Why BroadcastReceiver works even when app is in background ?

后端 未结 9 907
感动是毒
感动是毒 2020-12-04 13:59

I am checking Internet connectivity in my app using BroadcastReceiver and I show an alert dialog if the connection is lost. It works fine. But my problem is that BroadcastRe

9条回答
  •  既然无缘
    2020-12-04 14:16

    A simple way of finding whether the app is in foreground or not

    if((mContext.getPackageName().equalsIgnoreCase(
                    ((ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE))
                            .getRunningTasks(1).get(0).topActivity.getPackageName())))
    {
    //app is in foreground;
    }
    

提交回复
热议问题