Getting current running application
You can try this code it gives the package of the currently active screen:
ActivityManager am = (ActivityManager) this
.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if(!componentInfo.getPackageName().equals("your.package.name"))
{
//Do your stuff
}
You might need the permission
<uses-permission android:name="android.permission.GET_TASKS" />
How to find the currently running applications programmatically in Android?
ActivityManager activity_manager = (ActivityManager) context
.getSystemService(Activity.ACTIVITY_SERVICE);
ActivityManager
has method getRunningTasks(int)
. ActivityManager
seems to be the solution you are searching for.
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++)
{
Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");
}
Also, have a look at following thread:
See Android recent task executed by the user
Related Topics
Adb' Is Not Recognized as an Internal or External Command, Operable Program or Batch File
Android- Create JSON Array and JSON Object
Android Asynctask Threads Limits
Google Play Services Library Update and Missing Symbol @Integer/Google_Play_Services_Version
Storing R.Drawable Ids in Xml Array
Android: How to Check If Activity Is Running
Force Application to Restart on First Activity
Changing Viewpager to Enable Infinite Page Scrolling
What Are Advantages of Setting Largeheap to True
How to Check Current Running Applications in Android
Start_Sticky Does Not Work on Android Kitkat
Huawei, Logcat Not Showing the Log for My App
How to Change the Android Actionbar Title and Icon
Dynamically Add and Remove View to Viewpager