How to get current focus view object in Service? - android

How can i get the current focus view in my Service.
I found some methods to do this, like Activity.getCurrentFocus() and
Windows.getCurrentFocus().
But these methods are used in a Activity.
If I want to use these, I have to get the current focus activity.
I tried to use WindowManager to get current task.
It only got the current Activity's class name in this way. not
Activity Object.
List<RunningAppProcessInfo> rList =activityManager.getRunningAppProcesses();
RunningTaskInfo rt = rtList.get(0);
rt.topActivity.getClassName(); // the class name of the current Activity
How can I use these methods to get the current focus View in a
Service? Or is there another way to
do?

A service is a background work, so is not tied to any UI.
What you can do is bind your service to an Activity and call some method (through an interface) on this activity to return the focused view of this activity.
Anyway, can you explain better what you want to achieve? normally a Service don't apply to do things with UI, so maybe there is a better way to do what you want.

Related

How can we access an android activity from within context?

Trying to call a function from an activity, but I need the specific instance of that activity to do so.
Is there a way to call that specific activity from the application?
If not, is there a way to start an activity from the application so that I always have access to the instance I start running? I tried this, and edited the manifest, but the app never started...
As concerned with the limited details in this question, I think your requirement is to call a function in an activity that needs that activity itself as the parameter. I think you can do it like this.
Activity actiity=this;
yourMethode(activity)
{
//body of your methode
}
Whenever you use the variable "activity", you can get an instance to the current activity.
I think you are talking about casting the context to get the type of activity it represents. You can do it like this. But be careful if the context is not of that type you will most likely cause a crash.
((MainActivity) mContext).myMethod();
This is not really recommended as it will cause some tight coupling between the class and the activity.

Android: Visual A/B Testing library

I am developing a small A/B Testing library for Android. Library will only be initialised in application class. I need to change TextView values.
I will store all the data fetched from the in a file. But I am not able to track when ever a TextView gets into view and moves it.
For example TextView A is in X Activity, TextView B is in Y Activity and TextView C is in Z Activity. Since the variable I have is Context, how should I change TextView A, B, C values.
I need to figure out which Activity is Visible. From the Activity I will be able to get root view. And I will iterate over child views and change value. But How should i listen to Activity Change.
Is there any other approach to this ?
I know this is possible as many A/B testing library are doing this.
Here you go, check out my answer over Here
As you've mentioned, hooking into the activity lifecycle callbacks via AppContext is the best way to start. From there, you'll have all of the information you could possibly need. Every time the activity switches, you'll have the Activity object, and from there you can get the root view and apply changes as necessary.
I would advise against an iteration over the views though! If you have the rootview, you can just do a findViewId(textview C id) on that root view and you'll grab your view!
Since you are building a library, you can expose a function which can be called after onCreate of each activity, which will give you reference of the activity. Once you have the activity, you can get it's root view and do whatever magic you want to do.
That's the only other approach if you don't want to register LifeCycle callbacks for activity. The application needs to enter your library at least at some point of time. Either you can make the application do manually ( above approach ) or you can override all life cycle events of all activities ( registering lifecycle call back ).

Asynctask in Fragment or Parent Activity?

I have a scenario and I an not sure on what path to go.
Scenario
The app has a Home activity which displays various fragments. The data in the fragments can come either from the web or a local database and is retrieved using an asynctask.
From what I saw, I have 2 alternatives:
Put the Asynctask in parent activity and then use fragment.newInstance(parameters) to pass the result to the fragment. However, if in my asynctask I need to update the progress or some info on the fragment, each time I will have to call newInstance with the new set of parameters.
Add the fragment and put the asynctask in it, in this way when progress is needed, I can update the fragment's views, as I have access to them + when the asynctask is done, I can populate the list with the info.
What would be the correct approach ?
LE: actually for point 1 in order to update the fragment I can call fragment's public methods after I find it with findFragmentById in the parent activity
A better way if you have multiple tasks would be to use an IntentService :
http://mobile.tutsplus.com/tutorials/android/android-fundamentals-intentservice-basics/
You would have a better control to what you're requesting and what you want to cancel.
I would go with the second approach.
My primary reason though would be to avoid the issues that can happen on screen orientation change while the AsyncTask is working.
I would go with method 2, but take it a step further.
Have a separate fragment to run your async task. This way, you can handle any configuration changes (not just rotating screen) without any issues.
In another fragment, you can display the data. You can pass the data from your async task fragment via callbacks to the activity, and have the activity call a method in the display fragment to update the data.

How to get the activity reference from the ComponentName

I have the ComponentName of a foreground screen activity. I am able to get the context object from the same. But I need the activity instance.
How can I get the activity reference from the ComponentName.
All this code will be written in a service.
Thanks in advance.
You can not get Activity instance from it's component name.You can store Activity instance as static field in a class(when onCreate method of Activity invoked) and get that field in service.
Also what do you want to do with instance of Activity in a Service?
I'd need a little more clarity on what your attempting to do, but possibly a:
class ActivityWatcher using the onActivityStarted method (or similar)
to call a class that watches your view Hierarchy
Watch the parent view and then iterate thru the child views.
This would at least give you all the views from the activities layout that are defined at the time the activity is instantiated.

Access Activity in a static way

I have an activity which has a static method for updating a textfield. This way I can update this view from another activity.
But now I'm trying to get a Context variable in this static method which is not possible. I've tried declaring a Context variable and initialising it in onCreate ( context = getApplicationContext();)
But still I can't access context in this static method. How is this normally done?
edit: a little bit more information about my situation. I'm starting a countdowntimer in an activity(a) which updates another activity's(b) ``textfield every second. And it does this by accessing b's setTextField in a static way..
How is this normally done?
Accessing a TextView via a static method is not the best way to update the field from another activity. If you want to pass a value to the activity when it starts, you can send data via the intent (i.e. intent.getExtras). If you want to pass data back from a sub-activity, you can use startActivityForResult.
The way you are going is very strange. Why are you trying to change one activity content from another? May be you need to use startActivityForResult to strat a new activity and then return result from it and change views depending on it?
You might want to check some documentation on OO and using static functions. It is not considered a very good approach.
But as we are not talking about a better complete sollution: you can add a parameter with a context to the function, and just give it when you call the function :)
I would suggest the LocalBinder pattern to update the other Activity:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html
Can you do something like this?
something like this <viewobj>.getContext()
Ref: How can I start an Activity from a non-Activity class?
Whenever you're busy with Activity A, there's no point in updating something on Activity B as it is simply not shown to the user at that point in time.
Seems to me you need to have some kind of global variable here that can be picked up in the onResume of Activity B.
Checkout this question : How to declare global variables in Android?
It shows you how to use the Application class to maintain global application state, accessable from all activities when needed.

Categories

Resources