/**
* Check if target Activity is on top.
* @param context
* @param targetAppPackageName
* @param targetAppActivityName
* @return
*/
private boolean isTargetActivityIsOnTop(Context context, String targetAppPackageName, String targetAppActivityName) {
String TAG = "Check_Top_Activity";
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> mTasks = mActivityManager.getRunningTasks(1);//5.0
if (mTasks.get(0) != null){
ComponentName topActivity = mTasks.get(0).topActivity;
if(topActivity != null && !TextUtils.isEmpty(topActivity.getPackageName()) &&
!TextUtils.isEmpty(topActivity.getClassName())){
Log.w(TAG,"topActivity.getPackageName() -> " + topActivity.getPackageName());
Log.w(TAG,"topActivity.getClassName() -> " + topActivity.getClassName());
if(topActivity.getPackageName().equals(targetAppPackageName) &&
topActivity.getClassName().equals(targetAppActivityName)) {
Log.d(TAG,"target Activity is in top !");
return true;
}else {
Log.w(TAG,"target Activity is not in top.");
return false;
}
}else {
Log.w(TAG,"topActivity is null or PackageName is null or ClassName is null.");
return false;
}
}else {
Log.w(TAG,"mTasks.get(0) == null ");
return false;
}
}