get current app running android (app blocker) - android

I am trying to develop an app in android that can block other apps on a specific time. I have found several questions on stackoverflow talking about this, but the solutions they provide are deprecated and nowadays not working. I am actually a little bit lost, so any ideas would be appreciated. The steps are (for the moment):
List all installed apps.
Detect the name of the app that is currently running (in a service).
Thanks!

Here are the answers to your question.
List all installed apps.
PackageManager pm = getContext().getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
Detect the name of the app that is currently running (in a service).
There is currently no way to detect or get info on the currently running apps or services. This was depricated long ago because of security reasons.
getRunningTasks
public List<ActivityManager.RunningTaskInfo> getRunningTasks (int maxNum)
This method was deprecated in API level 21.
As of Build.VERSION_CODES.LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak person information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive.
Note: this method is only intended for debugging and presenting task management user interfaces. This should never be used for core logic in an application, such as deciding between different behaviors based on the information found here. Such uses are not supported, and will likely break in the future. For example, if multiple applications can be actively running at the same time, assumptions made about the meaning of the data here for purposes of control flow will be incorrect.
Check it here for more info
Hope this helps.

Related

How to pull running services in Android 8/O (Oreo) after getRunningservices(ActivityManager) deprecated

I am trying to find a workaround to the getRunningServices method that is deprecated in Android 8 (O). I am interested in finding, from a background service, if a specific service is running. (I am not the owner of the service that I am interested in finding).
Is there any workaround for that method? How are other apps pulling those details in Android 8 like Antivirus or Performance Booster Apps?
pull running services in Android 8/O (Oreo) after getRunningservices(ActivityManager) deprecated
The short answer is NO.
As the getRunningServices document said:
As of Build.VERSION_CODES.O, this method is no longer available to third party applications. For backwards compatibility, it will still return the caller's own services.
It is said here that the method will not be available for third-party applications.
Notes: this method is only intended for debugging or implementing service management type user interfaces.

Android O Replacement for getRunningServices

In previous versions of Android, I used this method to find if a service from another app was up and running. It worked reliably for me:
ActivityManager manager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> services = manager.getRunningServices(Integer.MAX_VALUE);
However, with Android O, this is now deprecated and will only return information about the calling app's services. I've looked into other solutions, but I don't want to ask the user for more permissions, (UsageStatsManager, NotificationManager, etc).
Is there an alternate solution for obtaining if a service from a different app is running or not in Android O?
It is intentional that there is no replacement for this function. It is not too clear from the documentation, but from the docs:
Note: this method is only intended for debugging or implementing service management type user interfaces.
Basically, using the method to check other apps services was an unintended side-effect, so Google decided to remove it. Likely for privacy/security purposes to avoid apps "sniffing" other apps/services on the user's device.
You didn't mention your use-case, but you want to avoid asking users for permissions. However, Google is intentionally forcing developers to explicitly request permissions they need, a philosophy which you can find explained here.
As per documentation:
As of O, this method is no longer available to third party applications.
For backwards compatibility, it will still return the caller's own services.
But may be this solution will help: How to check if a service is running on Android?

ActivityManager topActivity not working with lollipop

I have an app that is using the topActivity call to timeout after a period of inactivity between apps, which worked fine on android 4.x but since the update to 5.x this has stopped working.
The full call is
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
Has anyone experienced this before or have a solution? There are no errors it is just not returning what is expected.
Note: This is not for a commercial app, this is for a very limited, managed distribution.
Quoting the documentation for getRunningTasks():
This method was deprecated in API level 21.
As of LOLLIPOP, this method is no longer available to third party applications: the introduction of document-centric recents means it can leak person information to the caller. For backwards compatibility, it will still return a small subset of its data: at least the caller's own tasks, and possibly some other tasks such as home that are known to not be sensitive.

Default/System Android Processes

I am currently working on an Android project that monitors what applications a user is running and cross-checks the corresponding processes with a whitelist stored internally on the device.
In order to make this work, I need to know what the default or system processes for the device are so I can add them to the whitelist. That being said, I have a few questions I was hoping you might be able to answer:
Is there a way to differentiate between a default/system process that MUST be running, and a process that belongs to an app on the device?
Are there different default/system processes depending on what phone/version of android the user is running?
If so, are those process names available somewhere for developer use? Or is there some other way to obtain them?
If I need to elaborate more please let me know, thanks for the help.
Let's say that you try ActivityManager and getRunningAppProcesses(). Iterate over that array of RunningAppProcessInfo objects and find those with importance of IMPORTANCE_FOREGROUND. If the docs are correct (haven't tried this), there should only be one process that is IMPORTANCE_FOREGROUND -- the one that is truly in the UI foreground.
(services can call a startForeground() method to get foreground priority, but I am guessing they have IMPORTANCE_PERCEPTIBLE)
You could then examine the pkgList of that foreground process and compare that against your whitelist.
However, this breaks down if:
Something pops up asynchronously (alarm clock app, incoming phone call, etc.)
An app that is logically in your whitelist has changes that affect its package name (e.g., developer released a "pro" app that a student paid for, and the whitelist only has the free app)
if the device has multiple visible items (e.g., Samsung's multi-window capabilities), if all visible apps are not IMPORTANCE_FOREGROUND
This at least gets rid of the problem of pure background stuff that the student cannot control, including your "default/system processes".
However, it requires you to continuously poll, which will be a serious detriment to battery life. That, plus the privacy implications, means to me that this app should, at best, only be used for exams, and should be something that the student can install shortly before the exam and remove shortly after the exam.
Well I think my team and I have come up with the best solution so far. After reading the Android Docs, we found that by using ActivityManager.getRunningServices() we can use the constant FLAG_SYSTEM_PROCESS to determine what processes are core system processes. Then all we would do is cross-check that with the total list of running processes to differentiate between them.

Find how much network traffic other Android apps generate

I am trying to make a background service which should measure traffic usage of various applications so as to be able to show to the user which apps consume most data traffic.
I found that Spare Parts app does exactly that, but after installing it on a 1.6 Dell Streak device I always get "No battery usage data available" for "Network usage". Does this function at all work in Spare Parts?
Also, I couldn't find a working source code for Spare Parts.
https://android.googlesource.com/platform/development/+/froyo-release/apps/SpareParts
looks to be outdated or incomplete. (?)
But Spare Parts seems to measure e.g. CPU usage per app. How does it do that on an unrooted phone?
My general idea of how traffic per app could be measured is to regularly check the
"sys/class/net/" + sWiFiInterface + "/statistics/rx_bytes"
"sys/class/net/" + sWiFiInterface + "/statistics/tx_bytes"
"sys/class/net/" + sMobileInterface + "/statistics/rx_bytes"
"sys/class/net/" + sMobileInterface + "/statistics/tx_bytes"
files and to see which app currently has focus and thus most likely to cause the generated network traffic.
Unfortunately I can't find how to get the app currently having focus.
I found this:
Android, how to get information on which activity is currently showing (in foregorund)?
but seems it's about testing, not just a 3d party service running on non-rooted Android device.
We can get what activities are running with ActivityManager.getCurrentTasks(), but any of them can be the one with focus. It seems like the Android architects explicitly don't want 3d party apps to know what app has focus, because of security concerns
(see http://android.bigresource.com/Track/android-zb2mhvZX4/).
Is there a way around this?
Also, if I want to not only detect which activities eat up traffic but also what services, I can get all currently running services with
ActivityManager.getCurrentSerives()
and even see for each one if it's in foreground mode (unlike to be thrown out if Android needs resources). But this again doesn't bring me any far.
Any ideas?
You can detect currently foreground application with ActivityManager.getRunningAppProcesses call. It will return a list of RunningAppProcessInfo records. To determine which application is on foreground check RunningAppProcessInfo.importance field for equality to RunningAppProcessInfo.IMPORTANCE_FOREGROUND.
But be ware that call to ActivityManager.getRunningAppProcesses() method must be performed NOT in the UI thread. Just call it in the background thread (for example via AsyncTask) and it will return correct results. Check my post for additional details.

Categories

Resources