Android: Listen for starting applications - android

I am looking for a way to listen for starting applications/activities.
I guess many of You know the application "Tasker" which achieves exactly that. If a chosen applications gets launched, a specific task starts.
Now I wonder how to achieve that? I could start a Service, but which method should I use?
I noticed that Tasker asks for a permission before using this method. The application redirects me to the Accessibility Settings tab where I can enable Tasker with the notification: "Tasker wants permission to: Monitor your actions - Allows TalkBack to know when you are using an app"
Any suggestions?
Edit:
I found out that an AccessibilityService is used. Now my problem is, how do I specify all installed packages to this Service? I know that it's possible to specify packages one by one but how do I select all?

you can use getRunningTasks Method
ActivityManager am = (ActivityManager) mContext
.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity
.getPackageName();
Note This method was deprecated in API level 21. you can try this also.

Related

How can I Detect an favorite android app started?

I want to implement an app which can detect what app launched, to do something with that!
for example I have a list of installed applications in my app and mark one favorite app. and when I start that marked app from default launcher or anyway, I could detect it and do something with that by a background service or broadcast receiver(For example launch a toast message).
How can I do this?
this isnt possible.. to be able to monitor all intents would make android extremely insecure
http://groups.google.com/group/andro...ddc9d36a24d77b
but there are ways to know when an application is launched. you just have to be creative.
this will give you a list of all the applications running.
Code:
ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
however to know when an app is launched you would need a timed loop and then check between versions of the List to see if there is a new app. this would suck the juice and be inneficient
AppProtector seem to access the eventlog. maybe you could have a ContentObserver attached to the event log
http://developer.android.com/referen.../EventLog.html
http://developer.android.com/referen...tObserver.html
EDIT
Interesting.
I also found this which solve your problem.
When you open any app from launcher below code will return the info of opened app so now you need to compare a package name with your favourite app package name which you already stored in your app database.
Code:
String str = ((ActivityManager.RunningTaskInfo)this.am.getRunningTasks(1).get(0)).topActivity.getPackageName();

Android app that kills background processes to save power

I am writing an Andoid app so that when battery life gets below a certain level, a dialog with options of how to save the battery appears. One of those options is to close all background apps/services (processes) using ActivityManager.killBackgroundProcesses(). The code is shown here:
public void TaskKiller( View view){
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)this.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
}
However, when I click the button that calls TaskKiller() and closes the background processes, some of the apps (Email, Google Maps) instantly begin he process of restarting. How can I alter my code so these apps stay closed until they are reopened? Also, is this approach sensible in regard to saving power or am I attacking this the wrong way?
I don't think that's the right way of handeling the problem.
These apps have broadcast receivers, which mean they'll restart the service whenever something happens (i.e. AC plugged in/WiFi turned on), and I don't think there's a way to stop that without root, and actually disabling the broadcast receiver.
You could make something that kills it every 5 minutes, but that wouldn't be very battery-friendly.
I don't think it's a good idea to force close the Maps app everytime, it's a bug in Android i think..
One of the answers is as following:
"
Actually, Maps always runs when you have "Backround Data" checkmarked in your General Sync Settings under Account Settings in your phone's Gmail app. Syncing backround data is necessary, unfortunately, in order for your phone service provider to provide calling and texting (although internet access will still work without this item checkmarked). Unchecking this box will remove Maps from Running applications (& any other app that needs it), improving battery time and speeding up your phone. But, if you want to make calls, text or use apps that require Backround sync, you have to have this ckeckmarked. If all you want to do is browse the net...uncheckmark it. There are currently no other legitimate solutions to the issue. Hope this is helpful...
"
See this issue (https://code.google.com/p/android/issues/detail?id=10251)

Can I use android app to track user activities?

I wish to know is it possible to write an android app that when it runs at the background, it can track user activities?(Such as what other app did the user used, what phone number did user dial, the GPS location for user, etc) Cause I am not sure can a single android app react to other application, does anyone know the answer? Thanks
In the general case, no, you can't. And users would probably prefer it so.
Once this has been said, there are certain partial solutions. Sometimes the system is so helpful that it will publish Intents reflecting user actions: for example when the user uninstalls an app -- with the caveat that you don't get that intent on the app itself being uninstalled.
It used to be the case that before Jelly Bean (4.1) apps could read the log that other applications publish and try to extract info from there, but it was a cumbersome, error prone, ungrateful task. For example, the browser shows nothing when it navigates to a certain page. You may read the logs for a while with adb logcat to get a feeling of what was possible and what isn't. This action requires the relevant permission, which cannot be held by regular apps now.
Thanks to #WebnetMobile for the heads up about logs and to #CommonsWare for the link, see the comments below.
Yes you can.
You can look here for instance about phone info:
Track a phone call duration
or
http://www.anddev.org/video-tut_-_querying_and_displaying_the_calllog-t169.html
There is a way to let Android and users know you are using and accessing their data for them to determine if they will allow it.
I am unsure you can simply access any app, but in theory if you know how to read the saved files that might be possible.
For instance Runtime.getRuntime().exec("ls -l /proc"); will get you the "proc" root folder with lots of data you might need there. This might have been changed, I am not sure, and I also don't know what you need.
Perhaps to get running process try:
public static boolean getApplications(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
}
For this to work you should include this in your AndroidManifest.xml
<uses-permission android:name="android.permission.GET_TASKS" />
See more about it: http://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses%28%29
You certainly could but I think reporting that data back to you, unbeknownst to the user, via the internet, would be considered spyware and almost certainly illegal in most jurisdictions.
Fortunately spying users at that level should not be possible. Certain features can be achieved with abusing bugs in android which sooner than later will be fixed. I see absolutely no reason for you to know what number I am calling and where I've been lately. It's basically none of your business.

Aware of other Apps' start

What I want is: when there is an App start, my App can know it and do something about it.
I had look up the android API but can't resolve it.
Is there any way to make it possible?
Not really. The general philosophy of Android is that one app should not affect the behaviour of other apps unless it's a system app or a device administrator. And there are no APIs for this for device administrators, so you can't do anything about it. Unless, of course, you modify the platform.
What are you trying to do?
Please explain what do you mean by "do something"
However there is a topActivity field defined in the RunningTaskInfo class. You can get a list of running tasks via the getRunningTasks(int) method in the ActivityManager and can traverse through that list to find the currently active task by checking the topActivity field.

Listen to ActivityManager events?

is it possible somehow to listen to the events of the ActivityManager, e.g. when activities are started? Does the ActivityManager send broadcasts? I havn't found anything indicating that it does.
What I basically need to do: I want my app to launch one of my activities whenever a certain (thirdparty) app is launched/takes focus. Problem is this needs to happen before the thirdparty app is actually displayed.
What I have tried so far as workarounds:
Logcat output: I query logcat every 0.8s (filtered to show ActivityManager events only) but this eats up to many ressources
getRunningTasks: Slows down the phone a lot too and is not very safe, as an activity might be running but not currently in focus
Any ideas?
I suppose there is no actually other legacy way to handle glabal system state, only
(ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.getRecentTasks() - Return a list of the tasks that are currently running, with the most recent being first and older ones after in order.
For details check docs
Perhaps though Android is a Linux you can run system tools like
Runtime.getRuntime().exec("ps -aux | grep smth")
But I think it would be hard to detect particular java application.
I think you can use the launch mode to determine which activity to launch to top level. Please check the question: Android singleTask or singleInstance launch mode?. Maybe it will help you.
I had a look in the android source, but there doesn't seem to be any events broadcasted.
https://android.googlesource.com/platform/packages/providers/ApplicationsProvider

Categories

Resources