Get the application that is running in the foreground

0

I tried using the following code to get the application running in the foreground from a service being executed from an application created by me, but the only thing I get is to get the name of my application in the foreground when it is running and for all the more either gmail, camera, gallery the only thing I get is com.android.google.launchergel .

I would like to be able to get the name of the application running in the foreground at runtime.

Then I leave you the code that executes the code of the thread that invokes the service for this task ...

public int onStartCommand(Intent intent, int flags, int startId) {
    // Let it continue running until it is stopped.

   // Toast.makeText(getApplicationContext(), "Service Started", Toast.LENGTH_LONG).show();


    running = true;
    Runnable r = new Runnable() {
        @Override
        public void run() {


            while(running) {

                ActivityManager activityManager = (ActivityManager)MyService.this.getSystemService(ACTIVITY_SERVICE);
                List<ActivityManager.RunningAppProcessInfo> taskInfo = activityManager.getRunningAppProcesses();
                for (int i = 0; i< taskInfo.size(); i++) {
                    String current = taskInfo.get(i).processName.toString();
                    Date currentTime = Calendar.getInstance().getTime();
                    Log.i("Line: ", currentTime.toString() + ";" + current);





                SystemClock.sleep(2000);

            }
            stopSelf();

        }
    };

     t = new Thread(r);

    t.start();





    return START_STICKY;
}
    
asked by Daniel 15.05.2018 в 16:52
source

0 answers