0

I am creating a productivity app which needs to know which apps is currently being used by the user. This is the code pertaining to the foreground service which I am using:

public class DemoService extends Service {
    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("obscure_tag", "DemoService has been created!");
        startForeground(1, createNotification("Click me and turn me off", "I keep track off background tasks like reminders which you set in conquer", "foreground_services", NotificationCompat.PRIORITY_MIN).build());
    }

    public NotificationCompat.Builder createNotification(String title, String content, String channel_id, int priority) {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channel_id)
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle(title)
                .setContentText(content)
                .setPriority(priority);
        return builder;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.d("obscure_tag", "DemoService onStartCommand has been triggered!");
        final Handler handler = new Handler();
        final int delay = 5000;

        handler.postDelayed(new Runnable() {
            public void run() {
                ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
                for (int i = 0; i < runningAppProcessInfo.size(); i++) {
                    Log.d("obscure_tag", "currently running app: " + runningAppProcessInfo.get(i).processName);
                }
                handler.postDelayed(this, delay);
            }
        }, delay);
        return START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

I know that this service is getting triggered since all the logs are appearing in logcat. The problem is that I expect getRunningAppProcesses() method to give me the name of the application which the user is using currently but I am only getting the name of my app "com.conquer_app" in the logs..I am using an android 10 device for testing this code. This happens even while I am using other apps. Where am I going wrong? PS: I have referred to this answer but the getRunningTasks(int) method is deprecated..Please guide me as to what I should do.

Pro
  • 445
  • 4
  • 14
  • "I am creating a productivity app which needs to know which apps is currently being used by the user" -- this is generally impractical on modern versions of Android, for privacy and security reasons. – CommonsWare Jan 08 '22 at 12:05
  • If that is so how do app blockers work? – Pro Jan 08 '22 at 14:22

0 Answers0