I want to get package name of application being displayed on screen at that time. I am using foreground service where i used the method below, but that only gives me mycurrent application's packagename if I opens any app e.g youtube,camer etc, and the mobile's com.huawie.android.launcher if I am at home screen.
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.RunningTaskInfo foregroundTaskInfo = activityManager.getRunningTasks(1).get(0);
packag= foregroundTaskInfo.topActivity.getPackageName();
Log.i("pack",packag);
Well, below API-Level 21 or loolipop, you can get the whole package with the activity class, but from API-22 you can't get the whole application package with activity because of some security reasons.
As you know that Android is not going to be a secure phone, so that's why activity name is restricted.