Is it possible to get an Activity object from other apps? - android

I need to get the Activity from all the other installed apps on the device, possibly the launcher activity, but all of them is ok too. I know this code:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.testapp.test");
that gives me an Intent object, but is there an equivalent to get an Activity object? Or some other method?
I've seen many topics similar to this one, but all of them return only the Activity name, the Intent, or a Context.

but is there an equivalent to get an Activity object?
No. Those other apps are in other processes. While those processes may have Activity instances, you cannot access them, because those instances are not in your process.

No.You cannot access Activity instance into other apps.Because both apps considered to be a separate process

you cannot access the context of activity of another apps but if you want to take some result and post some data to other apps then with help of Intent you can do that and get result in onActivityResult() of your activity,s context.

Related

Starting another app's activity

If I start another app's activity with an implicit intent, what does the activity back stack look like and why? I found in the docs that 'the activity runs in a separate process' - does this mean the activity is in its own backstack?
The backstack would still be your activity. Running in a separate process means that they run in separate Linux processes- that they have memory protection and cannot access each other's variables (they communicate by Bundle, which is passed from one to the other via IPC).
I guess it really depends on the flags that you pass in. Try going through
the developers for more info.

How to get the foregrund activity instance?

I try to get the foreground activity for a long time, and i didn't managed to get it until now.
I don't know if it even possible, but i am dont intersted in my app activity only.
There is no data transfer between my service and the activity which i want to get.
I saw lot of questions of this kind but i got nothing suitable for my needs.
I just need to get an instance, not a ComponentName, not decription of the current foreground activity.
I've tried through ActivityThread, ActivityManager, ActivityManagerService (even though i couldnt get his instance too), and so on.
Field activitiesField = activityThreadClass.getDeclaredField("mActivities");// won't help
activityManager.getRunningTasks(rnd);// won't help either
If there is any refelection way, listener or something like that, but not a static field.
Without knowing why you want an instance of the Activity, or other background info. I'd suggest the following.
If you're inside a Fragment, then you can do getActivity() - which will give a reference to the Activity, and you can then cast this as your own Activity.
Otherwise, you might want to consider having a BroadcastReceiver, which can start an Activity for you.
You shouldn't be accessing an Activity directly. If you have methods/logic you need to access, you might consider refactoring them into a helper class.
Edit:
"Your application runs in a secure sandbox environment, so other processes on the system cannot access your code or private data." Take from the official Android docs

Why use an intent to create an activity?

Folks,
This is a newbie question. I have read a few articles on intents but I am a bit confused on what the main idea behind an intent is when it comes to starting an activity. If I know that I have to create and show an activity, why can't I do something as simple as the following?
MyActivity a = new MyActivity();
a.show();
Thank you in advance for your help.
Regards,
Peter
A activity has a lifecycle and this is managed by the framework. I would say to get an extra hold of the life cycle, Android introduced a set of rules to launch a activity (startActivity). To add-on, Intent is not only to launch your activities. Intents can be used to launch other thirdparty or inbuild views/service/targets. This could be another reason why they introduced intent. Just my two cents.
Intents communicate between activities in an app and between apps.
Your example:
MyActivity a = new MyActivity();
a.show();
assumes that Android is just objects, so that instantiating an Activity and somehow showing it will make it appear. This isn't the case, though; the Android system does a lot more. The activities in your app are "floating", as it were, within the Android framework. Most of what makes an Activity tick is invisible to you. In particular, the Android-specific thread model and the way that the system communicates with Android components (like Activities) is invisible.
I won't go into most of this, but an added advantage of Intents for starting an Activity is that an Activity can add itself as a candidate for Intents that want to do a specific task. Suppose I have an app that edits images. I can easily make myself discoverable by filtering for Intents that have the action ACTION_EDIT for MIME types that I can handle. This is exactly how Android implements the list of apps that appear when you try do to something with a file.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
click here for more information.
in basic words Intent is your ears regards to Android device. Your activity can talk to Android through Intent and listen on "any change" on your phone.
It's like "grandmother" that sits outdoors next to entrance of big house and listens about all talks, gossips and notify you about.

How to get class name of the Activity which is bound to a Service?

I would like to get the class name (*.class) of an Activity which is bound to my local Service. Is there any native method which i could use to achieve this?
I tried to get that information out of the Intent I use to bind to the Service. But it seems there is no function in Intents for this.
If I wasn't blind, additionally I would like to know why there's no information about the calling Activity in an Intent?
Is there any native method which i could use to achieve this?
No, in large part because the class in question may not exist in your app, and may not even be a class.
You are welcome to package whatever identifying information you would like in the Intent, such as via an extra.
Also, if you are allowing third parties to bind to this service, you can use methods like getCallingUid() on your Binder to find out information regarding the process that bound to you.
additionally I would like to know why there's no information about the calling Activity in an Intent?
Because there is not necessarily a calling Activity. The binding engine works with C; C does not have classes, let alone activities.

Android link to another package's function

I've got two android apps, one with a public void. How would the other app call this function?
You can not directly call the functions in another application. There are two ways to to achieve something like this.
The more complicated way would it be to have a service running that both your apps bind to to communicate. This would only be the way to go if you have frequent data sharings etc.
Another method is to use startActivityForResult. This will redirect the user to the activity of the other application and if the activity is finished you will be returned to your old activity and the set result is given to you in onActivityOnResult

Categories

Resources