Get the status of the current task - android

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();
}

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)

Floating activity draw on other app upon App Launch

I will explain my app first. My app consists of a service running in the background. The service is waiting for a certain app to be opened (spotify in my case).
Whenever Spotify opens it needs to show this popup with buttons and text. It has to basicly look like this: imgur.com/QHWZpu6
I've already tried DialogFragment but that doesn't work since there is no FragmentManager in the service class.
Do you guys have any suggestions? Thanks in advance!
Detect if App is Running/Launched
Take a look at ActivityManager
The method you are concerned with is ActivityManager.RunningAppProcessInfo(). It returns the package names of current running apps as a list. All you have to do is iterate through list and check if it matches app package, which in your case is spotify.
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> appInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < appInfos.size(); i++)
{
if(appInfos.get(i).processName.equals("package name"))
{
//USE INTENT TO START YOUR ACTIVITY AS EXPLAINED BELOW
}
}
Start Activity from Service
To start activity from your service, use Intent as following:
Intent popUpIntent = new Intent(this, MyActivity.class);
popUpIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(popUpIntent);
Popup Activity / Show as Dialog
In order to display your Activity as a dialog, you just need to set your activity theme to "Theme.Dialog" in your manifest file
<activity
...
android:theme="#android:style/Theme.Dialog"
...
/>
Or you can dynamically set the theme in the activity by calling setTheme() inside Activity class.
setTheme(android.R.style.Theme_Dialog);
A service class is not supposed to interact with the UI thread. A simple interaction to get past this is to use the service thread to update something stored in SharedPreferences and set a shared preferences change listener in the UI thread for whichever activity you want to handle the UI display.
void onSharedPreferenceChanged (SharedPreferences sharedPreferences,
String key)
Based on that image, it seems you actually want to display a UI element when you're activity is not currently the active application, you might want to consider a notification for this instead

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"

How to get additional Information with ActivityManager

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.

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