How to get additional Information with ActivityManager - android

List<ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
activity = taskInfo.get(0).topActivity.getClassName();
packagename = componentInfo.getPackageName();
I have this code to get the running activity and package names. My question is:
This package contains another class (Name.java), which is not an activity and returns a String Value that I also need to get.
Is this possible with the ActivityManager?
To be more clear:
the home activity is a screen with a list of items. by sliding to the right or to the left, the screen changes and displays, for example, favorite items, rated items etc. each of those screens/views have a name (Name.java handles it). What I want to do is, to get the name of the current view also, not just the name of the Activity.

No, you can't get this information using the ActivityManager. The ActivityManager will only give you information about Android components (Activity, Service, BroadcastReceiver, ContentProvider) and their configuration. You can't access random Java classes within other packages.

Related

why is android coming from background activity class "com.miui.home.launcher.Launcher"

While the activity is on, I go to the background and when I want to learn the classname from within a service, I see "com.miui.home.launcher.Launcher".
I need the activity class. Where am I going wrong?
The codes I wrote for classname are as follows;
ActivityManager am = (ActivityManager)
getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
String className = componentInfo.getClassName();
com.miumi.home.launcher.Launcher is Activity set as devices launcher - "desktop" app responsible for showing all your apps grid, widgets etc. when your app is in background (put there by e.g. Home button) and any of your Activites isn't present on the screen then your device is showing Launcher app and this is reporting your code
note that getRunningTasks method is deprecated and won't let you know which third-party app is on foreground. currently this method may only return info about your app or Launcher/"desktop"
https://developer.android.com/reference/android/app/ActivityManager#getRunningTasks(int)

Get the status of the current task

When I mention task below, I am referring to a specific Android concept here:
http://developer.android.com/guide/components/tasks-and-back-stack.html#TaskLaunchModes
Hi there,
Let's say there is a button in a base activity. When this button is pressed, a third party file viewer activity is launched from base activity through intent, which displays a file to the user. We do not set any flag in this intent, so I assume the viewer activity is in the same task as the base activity.
So when this viewer activity is in the foreground, is there anyway to detect Home button press, since this triggers the status of the current task to change (probably from having one activity in the foreground to having all of them in the background?)?
Any thought/idea/comment is highly welcomed!
Thanks!
This code at least get the name of the current top activity:
ActivityManager am = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
topActivityName = tasks.get(0).topActivity.getPackageName();
}

Get chooser activity name in android source

In my application, I'm checking the top activity name in background service and call a function if top activity is not Chooser activity in android by default. I found that activity name to be "com.android.internal.app.ChooserActivity". So I'm hard-coding that and checking. Everything is working fine. But what happens when that activity name gets changed(Changed by google). So my question is, Is there any way of getting the chooser activity in run-time?
In your background service run a thread getting the top of the activity via ApplicationManager then you can compare the top of the activity during runtime.
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::"
+ taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
You will need the following permission on your manifest:
uses-permission android:name="android.permission.GET_TASKS"

How to check whether the activity I am starting is the activity which is running?

I have 6 Buttons, for 1st 3 Buttons I want to start same activity and for other 3 Buttons separate new activities.
So problem arises when i click on 1st button and then the second Button which starts same activity because we can not start same activity from same activity.
So I just need to know weather the activity which i am going to start is the same activity which is running, based on that if same activity then I'll just update some UI contents else start new activity.
So anyone can help me to know this?
You can check activity's class, for example (suppose 'this' is current activity):
if (this instanceof ActivityClassYouGoingToStart) {
// update UI
} else {
// start new activity
}
Here's a good way to do it using the activity manager.
You basically get the runningTasks from the activity manager. It will always return the currently active task first. From there you can get the topActivity.
Example here
There's an easy way of getting a list of running tasks from the ActivityManager service.
You can request a maximum number of tasks running on the phone, and by default, the currently active task is returned first.
Once you have that you can get a ComponentName object by requesting the topActivity from your list.
Here's an example.
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
Log.d("topActivity", "CURRENT Activity ::"
+ taskInfo.get(0).topActivity.getClassName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getPackageName();
You will need the following permission on your manifest:
uses-permission android:name="android.permission.GET_TASKS"

add a view using the broadcast receiver to the top activity which is running currently?

Can i add a view to the top activity in broadcast receiver.
ActivityManager am = (ActivityManager) mContext
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
componentInfo.getClassName() returns the class name of the top activity running. I can also get the package name. Can I add a view or some animation on current top activity. I need the layout name in most cases to bind to it but I am not able to figure out how to do it.
You can't do that, it would be quite a security concern if you do. However you may be able to start a new activity that's on top of it and doesn't cover the entire screen? What are you ultimately trying to accomplish?
Ok . i figured out . I started a service in the background , and then i got hold of the WindowManager and then inflated the view to be added . Then i used windowmanager.addView(inflatedView) .

Categories

Resources