Activity Listener - Google Cloud Messaging - BroadcastReceiver

怎甘沉沦 提交于 2019-12-03 20:22:38

There are two ways to handle this.

1. Check if activity is running.

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(componentInfo.getPackageName().equalsIgnoreCase("com.yourpackagename")){
    //Activity Running
    Send a broadcast with the intent-filter which you register in your activity
    where you want to have the updates
} 
else{
    //Activity Not Running
    //Generate Notification
}

2. Using SendOrderedBroadcast.

This blog will give you an idea on how this could be achieved.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!