Click here to Skip to main content
15,907,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to detect the application launch in android application ?
How does the Smart App Lock applications in android detect the launch of an application ?

import android.app.ActivityManager;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.Iterator;
import java.util.List;

public class MainActivity extends AppCompatActivity
{
public  static  final String TAG ="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final PackageManager packageManager = getPackageManager();
        //get a list of installed apps.
        List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
        for (ApplicationInfo packageInfo : packages) {
            Log.d(TAG, "Installed package :" + packageInfo.packageName);
            Log.d(TAG, "Source dir : " + packageInfo.sourceDir);
            Log.d(TAG, "Launch Activity :" + packageManager.getLaunchIntentForPackage(packageInfo.packageName));
        }

        ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
        List l = activityManager.getRecentTasks(1, ActivityManager.RECENT_WITH_EXCLUDED);
        Iterator i = l.iterator();
        
        while (i.hasNext()) {
            ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) i.next();
            try
            {
                CharSequence c = packageManager.getApplicationLabel(packageManager.getApplicationInfo(
                        info.processName, PackageManager.GET_META_DATA));
                Log.w("LABEL", c.toString());
            } catch (Exception e)
            {}
        }
    }
}


What I have tried:

I tried this snippet to detect atleast, the applications currently running. But this throws me a Class Cast Exception :

android.app.ActivityManager$RecentTaskInfo cannot be cast to android.app.ActivityManager$RunningAppProcessInfo
Posted
Comments
Richard MacCutchan 16-Feb-18 4:46am    
Because the two classes are different.
David Crow 19-Feb-18 9:40am    
The exception is telling you exactly what the problem is: RecentTaskInfo cannot be cast to RunningAppProcessInfo. Check out line 74 of MainActivity.java.

That issue aside, why are you trying to use the deprecated method getRecentTasks()? See the warning here.
GaneshRfromSpace 20-Feb-18 4:47am    
Yea I realised very lately that both getRunningTasks(int) and getRunningAppProcesses() are deprecated from Android 5+.

I found this library here.

Is there any other way to know what all applications are currently running ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900