How can I Detect an favorite android app started? - android

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

Related

How to detect Application Launch

Can someone give the explaination about this question that how this detected application launch by catching startapplication calls in the launcher in the following question?
Detecting android Activity launches
Some code would be highly appreciated. I have tried the first two options but having the same problems. Kindly Help
Details:
I need to detect Application Launch in Android. That whenever any application is opened, my application should be triggered. This is very similar to what App Locking applications do.
I have written the following code in a service to get the currently running application:
ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(2);
String CurrApp = taskList.get(0).baseActivity.getPackageName();
The problem is that I need to detect whenever there is new application in tasklist i.e. when user opens any application.
Possible solutions and there drawbacks are given in the question that I posted. In this question, the perfect solution is to catch the startapplication calls in the launcher. I need to know how to do it.

How to check if certain intent has started

How to check if intent or app has started ?
My use case is I am showing up notifications through my app and I want to clear them all via myBuilder.cancelAll() if default messaging app has started since my app shows sms notifications. So I am kind of looking for something like:
if (smsAppStarted) {
myBuilder.cancelAll();
}
Thanks
To check if an app has started:
Get the package name of the sms app you want to check.
Then refer to my answer here:
Android how to know an app has been started and range apps priority according the starting times
By using that code, the list taskInfo will contain the list of all apps currently running. Search that list using the package name of the sms app. If it is present in that list, it means that that app has started and currently running.
If I get you right, you need to determine, if another app is currently running. If so, then use this solution.

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)

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.

how to get the pid of a package in android

I have done the coding on getting all the installed packages in the android system! now what I want is to get the pid of that package since I want the current resource usage of the package! I dont know the method of doing this, where I know the reverse of it! :) I dont want to use the shell either! is there a way to achieve this! or else I'll state what I want to do and suggest me a better way..
what I want :
I have created an activity, where I take all the current installed third party applications or the system. used the package manager to get it! I listed it on a layout and when I click on an application I want it to direct to another screen where all the details are shown about the application. I already have some details which i got using the package name of the application. the thing is, I want to show the memory info, and the code size of the application! how can I achieve this! :)
please help!
PID is denoted to Process ID in Android. And as you are telling you need process ID of all installed applications, But afaik PID is given to application/service/process which are running, So you can not ask for process ID for all installed apps.
You can get pids of all running apps, by using below code..
RunningAppProcessInfo service[]= manager.getRunningAppProcesses();
String = service.pid;

Categories

Resources