ActivityManager topActivity not working with lollipop - android

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.

Related

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.

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?

Xamarin.Mobile: task-based API marked as obsolete

The latest release of Xamarin.Mobile component obsoletes some Task-based APIs for Android. Release notes briefly comment on this:
Given the fragility of the Task<> based API on Android due to Activity lifecycle realities, the async API is now marked [Obsolete] specifically for Android.
Could someone please explain what fragility is meant here?
Essentially, using Task across app lifecycle boundaries is asking for trouble. When the camera Activity starts on Android, you are actually starting a completely new app. Your app is no longer running in the foreground, so Android is completely within its rights to kill off your app and just restart it when the camera returns. If this happens, your Task instance has been destroyed and so any awaits or ContinueWiths you have will never execute. It's not a Task/Android issue, it was simply a design flaw in Xamarin.Mobile.
As a result, the magic API was deprecated in favor of one that utilizes OnActivityResult, as it is the only way to properly handle this situation. If you notice, the new API GetMediaFileExtraAsync still returns a Task<MediaFile>.
(Source: I wrote Xamarin.Mobile).

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