Android O Replacement for getRunningServices - android

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?

Related

AOSP: Custom system service communicating with other services

I am investigating about a custom system service for AOSP to provide a basic remote control (switching users and starting apps) for the system via network. It should be based on Android 9. For future portability, I would prefer to use rather high-level Java APIs, if possible.
I don't have much knowledge about Android yet on system level. It seems, at least a part of the functionality can be covered by communicating with the Activity Manager, which could be a good starting point.
Some of my questions:
is it intended at all, to have different system services to communicate with each other?
Provided this is possible, how can one system service use other service's functionality? Should this still go through HIDL/binder although all services live in the same process?
Is there an existing system service which does something similiar that could be useful as reference?
is it intended at all, to have different system services to communicate with each other?
Yes, that is intended.
Provided this is possible, how can one system service use other service's functionality? Should this still go through HIDL/binder although all services live in the same process?
HIDL over /dev/hwbinder is intended for HAL to System Service communication. Communication between System Services can still be done with AIDL over /dev/binder. I think you would typically use a services Manager class which will abstract Binder use anyway. This might not be a nice minimalistic example, but you have a look at how the Car service uses the TelephonyManager in CarAudioService.java.
Is there an existing system service which does something similiar that could be useful as reference?
You can have a look at the additional services in packages/services.
There are two type of system services:
1. Running in system_server: they are started in SystemServer.java; apps can call them by XXXManager(eg, ActivityManager); and they also provide some internal api which called by other services in system_server.
2. Running in apps which have system uid or platform permission: they are normal app services; they can do something that third app can't do; they are complied with android source, so then can call hide api.
Type 2 services can meet most needs. So I suggest you to choose Type 2 service. It's more easier to debugged and maintained.

get current app running android (app blocker)

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.

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.

Quit application when there is a new update android

I was wondering which approach is best for forcing users to update my android application.
Should I just ask my server if there is a new version in the main activity and if there is just prompt a message and call System.exit(0); or is there is a nicer approach to this? Is it possible to check the playstore for new update? Or is this behavior of forcing update a bad practice? even if the update is important (that would make some features not work in old versions)?
Thanks
Generally, it is best to respect the user's choices. In Android, users can choose whether or not to enable automatic updates (and whether or not to update at all).
That being said, you wouldn't be the first developer to implement this functionality. If you do go this route, I would recommend calling finish() on your Activities instead of calling System.exit(0); to ensure that everything goes through the Activity lifecycle properly.
There is some great discussion about quitting an application in general at Quitting an application - is that frowned upon?.
Another alternative to forcing users to update your application when your application relies on a remote server (such as a PHP backend) is to provide backwards compatibility when you update the backend. You don't necessarily need to provide backwards compatibility indefinitely, but providing at least a few versions of support is probably a good idea.

Android 2.2 deprecates restartPackage but adds another headache

Android 2.2 release notes have just been released. ActivityManager.restartPackage method has been deprecated and the description is:
the previous behavior here is no longer available to applications because it allows them to break other applications by removing their alarms, stopping their services, etc.
Instead 2.2 has given another tool for pesky "task killer" apps by introducing new ActivityManager.killBackgroundProcesses method.
More Info
Can someone explain whether ActivityManager.killBackgroundProcesses will kill our scheduled alarms?
If so, deprecating ActivityManager.restartPackage was pointless as "task killer" will now abuse ActivityManager.killBackgroundProcesses.
I have made tests with this new killing method : alarms are not killed. services are restarting.
It also appears from my testing that user-visible activities are not closed when this method is called.

Categories

Resources