1

I have list of apps in my app, When i click any of them i want to start a service and that service it checks whether that app is foreground or not, if yes then it shows in the LogCat which app is been opened. So in this case i have used Service class only, and its not showing logcat message.

This is the Service class

   @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String openedApp = intent.getStringExtra("openedApp");
        if(printForegroundTask().equals(openedApp)){
        Log.e("FOREGROUND", "Current App in foreground is: " + openedApp);
             // startActivity(lockIntent);
        }
        return START_STICKY;
    }
    private String printForegroundTask() {
        @SuppressLint("WrongConstant") UsageStatsManager usm = (UsageStatsManager) this.getSystemService("usagestats");
        long time = System.currentTimeMillis();
        assert usm != null;
        List<UsageStats> appList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 1000, time);
        if (appList != null && appList.size() > 0) {
            SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
            for (UsageStats usageStats : appList) {
                mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
            }
            if (mySortedMap != null && !mySortedMap.isEmpty()) {
                currentApp = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
            }
        } else {
            ActivityManager am = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
            assert am != null;
            List<ActivityManager.RunningAppProcessInfo> tasks = am.getRunningAppProcesses();
            currentApp = tasks.get(0).processName;
        }
        return currentApp;
    }

and this is the MainActivity

   @Override
    public void onClickMethod(int position) {
        final String ApplicationPackageName =  adapter.stringList.get(position);
        Intent appIntent = new Intent(getApplicationContext(),MyService.class);
        appIntent.putExtra("openedApp",ApplicationPackageName);

        if(isLocked ==true){
            startService(appIntent);
            Toast.makeText(getApplicationContext(),ApplicationPackageName+ " LOCKED ", Toast.LENGTH_SHORT).show();
            isLocked=false;
        }else{
            stopService(appIntent);
            Toast.makeText(getApplicationContext(),ApplicationPackageName+ " UNLOCKED ", Toast.LENGTH_SHORT).show();
            isLocked=true;

        }

    }

i defined Service class in the Manifest and i gave the permission.

I expect to see the logcat message when the app is in the foreground. But its not showing any message.

Naveen
  • 202
  • 2
  • 11
ismail
  • 167
  • 1
  • 8
  • 1
    You want to know when an app goes in "background" and when it comes to "foreground". This is clear. But it is still not fully clear to me what exactly are you trying to achieve with "Service"? Do you want to have a foreground notification which shows if app is in foreground/background? And also keep check on status of multiple apps and not just one? What have you tried so far to "detect" the background/foreground status? What's happening in `printForegroundTask()`? – Wahib Ul Haq Sep 05 '19 at 09:26
  • Basically I'm trying to build an AppLock that locks the selected apps, So I want to know when an app goes in foreground only, In service class I'm trying to compare the the locked app in the list and the foreground app, If they both are same then i want to see logcat message. yes, I want to have a foreground notification which shows if app is in foreground and keep checks. `printForegroundTask()` supposed to check the foreground app but it seems its not working, i don't know. – ismail Sep 05 '19 at 09:41
  • And you don't have control on the "selected apps" right? you can't update their code as they are external apps? My initial thought was that you want to know how to detect foreground/background in your app because that is possible but if you want to build an app (service) which can detect which other unrelated apps are in foreground/background, then that's a totally different challenge. – Wahib Ul Haq Sep 05 '19 at 11:03
  • Oh! Do you have any idea how to detect other unrelated apps are in foreground ? – ismail Sep 05 '19 at 11:24
  • Nope, no idea. I just googled and maybe this answer has a clue https://stackoverflow.com/a/49743477/1016544 – Wahib Ul Haq Sep 05 '19 at 20:51

0 Answers0