How to detect Application Launch - android

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.

Related

Get running application flutter

I want to know the package of of the application currently running in phone in my flutter app, like the "Forest" application that when you open some applications an error pops up or a function is called.
How can I do that?
go to below link and read it, then u will understand flutter backgrounds how works.
Background with Flutter
Edited:
code blow is what u want in android, now just search and find what library or method do it in Flutter.
ActivityManager activitymanager =
(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
RAP = activitymanager.getRunningAppProcesses();
for(ActivityManager.RunningAppProcessInfo processInfo: RAP ){
Log.e("TAG",processInfo.processName);
}
this print List of background apps in Logcat.
hope that be helpfully for you.

How to know if Android user is interacting/using the phone by a background service?

I am making a battery app to monitor user's charging behavior. Basically, it is a background log server that saves your charge profile and analyzes it. Since it is possible that users is using their phones (e.g., playing games) during the charge , this changes the charge behavior. Thus it is necessary for my app to differentiate those data from the others.
Detecting if people is using the phone by a foreground app is trivial (you just check if user is touching your UI). However, I found it is relatively challenging for a background service to do the same thing. The only way comes to my mind is to check the brightness of screen. However, it is also possible that people leave the phone with screen on.
Could anyone give me some suggestions on this problem?
--------- Hi, here is a update of my question --------
First of all, the trick to get foreground app does not work after Android 5.0 (it is also posted getRunningTasks doesn't work in Android L). After Android 5.0, the "getRunningAppProcesses()" only returns your app. Based on the other discussion in Stackoverflow, There is an alternative to use app statistic but which needs system permission. Moreover, it also doesn't work at certain phones (in my test, most Samsung phone is unable to find the option to enable this function).
Second, I test the foreground app trick in some of my phones (before 5.0), but I think it doesn't meet the purpose of knowing if users are "interacting" the phone. For example, it is possible that users are running an app and just walking away for more than few hours but the foreground app trick still thinks the phone is in using (but it is not).
You can do it via ActivityManager.
Look at this piece of code :
Edit :
As of lollipop, getRunningTasks() has been depreciated. So, better use getRunningAppProcesses().
Sample code :
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return;
}
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
//do what you want to do
}
}
Credits for sample code : https://stackoverflow.com/a/8490088/1594776

Android: Listen for starting applications

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.

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

I would like to update my app UI, but ONLY if my app is in the foreground

I have an app that is very simple - it turns bluetooth on and off according to whether it's plugged in.
The app right now is also very simple in terms of UI - there is 1 button on there, that allows you to turn bluetooth on and off if you want.
I actually have this working, and have figured out how to update the button, but, I would like to update my UI only if the app is in the foreground - so, if I plug in my phone, and i'm NOT in my app, I don't want my app to pop up and update the UI - I only want the UI to change if the app was open when I plugged in the phone.
I've tried the following code, and it does find the app in the running apps list, but it opens the app no matter what - i.e., it'll open the app and update my UI whether it was open before or not.
ActivityManager activityManager = (ActivityManager) context.getSystemService( Context.ACTIVITY_SERVICE );
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
for(RunningAppProcessInfo appProcess : appProcesses){
if(appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND){
Log.i("Foreground App", appProcess.processName);
if(appProcess.processName.equals("vermel.BluetoothOn")) {
//update UI
}
}
}
What am I doing wrong? Please bear in mind, I'm a fresh newbie in both Java and Android, and would highly appreciate details explanation, as opposed to a link to the android development documentation ;)
Thanks for reading!
if you add #Override onWindowFocusChanged(boolean hasFocus) to your Activity you'll be able to track when you have focus and when you don't. you can then wait until you have focus to update your UI.

Categories

Resources