Get name of focused activity regardless of owner - android

Is there a way in android to get the name of the activity (package.activityclass) of whatever activity has focus? I don't have root permissions, but I'm willing to write this with Device Admin or Accessability if it calls for it.
For example, if the camera is up, you would get "com.android.camera.CameraActivity" (or whatever the class is.)
If such a facility doesn't exist, that would make sense too, considering the security issues that come with it.

I think the best you can do is use the ActivityManager APIs, like getRunningTasks()

Related

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

Detect when an external android app runs onCreate, onStart, onResume methods

I want to know how to detect when an external app runs one of this methods. I'm working with some classmates in a project where we want to examinate the response time of other applications. The idea is to measure the time between the run of each method to get an aproximation of the response time when opening the app.
Is this possible to achieve?
Android apps are sandboxed and only expose content that they intend to expose. The methods you name are part of components that cannot be accessed directly from the "outside" world. In other ways, if an app wanted you to know when those methods are being called, they will expose that information (i.e. sending a Broadcast or maybe storing the information in a ContentProvider). You can try and see if you can get some information out of the logcat, but I cannot assure how accurate and consistent it will be.
This is imprecise, but I would monitor logcat activity. Depending on the device/VM/AVD logcat is super active during transitions (such as back-grounding and foregrounding) and idle when an app is awaiting user input.
EDIT:
Other than that, if you can do your analysis off the device, perhaps look into using DDMS?

Get Currently Active User in Android

I'm looking for a way to get the current active user in Android. I'm building a system app so I can use hidden methods, but specifically it has to be the current ACTIVE user, and not the user for a given process.
For instance, if you install an app using ADB the process is going to say the user id is the owner. This means that UserHandle.myUserId() will not work for what I need :(
So I found a method that does exactly what I need it to do. There is a static method in ActivityManager that is hidden (so you need to work some magic to have access to it) but here it is:
ActivityManager.getCurrentUser();
That's all there is to it.

Call Back button from service Android (system-wide)

I need to remap the Honeycomb "Back" button to a button in my app (service) but after hours of search I'm still nowhere. The functionality must be there system-wise like the back button in ButtonSaviour (see market)
Most solutions for emulating the Back button are based on calling finish(). Not sure if it will work in my case since I have to call finish() from whatever activity I find on the foreground. I do manage to get the foreground application with the code
ActivityManager am = (ActivityManager) getContext().getSystemService(getContext().ACTIVITY_SERVICE);
List<RunningTaskInfo> T = am.getRunningTasks(5);
System.out.println("top activity: "+T.get(0).topActivity);
but I'm not sure how I should send a finish() intent to that..
I also tried the solution posted here http://www.anddev.org/throwing-simulating_keystrokes_programatically-t717.html but I couldn't get around the IWindowManager (has it been removed in Honeycomb?). That solution, however, looks interesting because with that I could send KeyEvent.KEYCODE_BACK from anywhere, anytime.
Please let me know which is the best way to implement this functionality SYSTEM-WIDE, i.e. from a service rather than from a specific application of mine.
cheers
PS: The app is meant to run on my own rooted tablet rather than for distribution to others.
Why does people spam the thread with advises about designs and good practices when the question clearly states that this will not be used for the market?!
Replacing android nav bar is sometimes required in some applications, even if no more than for own pleasure (or programmer satisfaction).
try from java Runtime exec
input keyevent 4
4 means back
button savior most likely uses hidden api, I had encountered that somewere, but don't remember now. When I will find it, I will post back.
The reason you're not finding an answer to this is that this is a terribly hacky way to do things and it indicates that there is something majorly broken with the design of your app. You should not be able to "call the back button," that doesn't make any sense at all. In fact, you really shouldn't change the behavior of the back button at all, users get really upset when you do that. If you have a service, and you need to send messages to an Activity, then use a messenger. But you should certainly not (and certainly cannot) be changing the behavior of other apps from your service. (This would indicate a major security flaw in the system, and would let you hijack someone else's UI, doing potentially dangerous, or at least annoying, things.)
Will the activity be open when you want to simulate the back button? If so, you could set up a timer inside your activity that checks for an exit file every 5 seconds or so. Then, from your service, create the exit file when you want to simulate the back press. When the activity sees that the exit file was created, delete that file and call finish().
As others have suggested, though, doing things this way means that you might need to rethink the app's design. What exactly are you trying to achieve?

Android Application Level Scope Variables

I understand that there is no Application Level Scope available to be able to define shared logic/data, and that each activity is essentially a stand alone application tied together by the manifest...
BUT I have a group of user access permissions that I am getting via a web service call and do not want to make this call in every activities onCreate().
I am using SharedPreferences in the application, but since the permissions are not intended to be editable by the user, the flat file exposure on android devices feels like an insecure way to handle this.
I do need to re-request this info if the application is restarted, so I believe the least expensive would be to store it in a variable.
I am aware of IntentExtras but we are talking about a Settings "Object", not a primitive type.
right way to handle this situation?
You can actually create an "Application" class that can be used to essentially create Application wide settings.
Just create a new class and extend Application, then set any class members and appropriate getter/setter methods and you can access them throughout your application.
You then need to update your manifest as follows:
<application android:icon="#drawable/logo"
android:label="#string/app_name"
android:name=".application.CustomApplication">
Then in any activity you can access it as follows:
CustomApplication app = ((CustomApplication)getApplication());
I think that using shared preferences is fairly secure. Only advanced users with root, custom roms and hacking knowledge would be able to take a chance at it! (I'm not even sure that this would be possible).
Besides SharedPreferences, you could also implement a custom Application object and keep your permissions there.
Anyway, as a developer I think that it's much more likely to be hacked somewhere within the request I do to get the user permissions (use https, etc) and my application being decompiled.

Categories

Resources