I want to capture the time whenever a user starts any application using my broadcast receiver. is it possible that a broadcast receiver can catch such an event?? if yes , is there any permision which can do that?
Asked
Active
Viewed 1.6k times
13
-
1you can code it in the the class that extends BroadCastReceiver – Lucifer Nov 09 '11 at 06:32
-
Kalpen : i want to invoke the broadcast receiver when the user opens any app, so wht do i write in d class tht extends the broadcast receiver – jehan Nov 09 '11 at 06:37
2 Answers
13
The best you can do is create a STICKY Service that tracks all of the running application.
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run()
{
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < services.size(); i++) {
if(!stalkList.contains(services.get(i).baseActivity.getPackageName()))
{
// you may broad cast a new application launch here.
stalkList.add(services.get(i).baseActivity.getPackageName());
}
}
List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++) {
ArrayList<String> runningPkgs = new ArrayList<String>(Arrays.asList(procInfos.get(i).pkgList));
Collection diff = subtractSets(runningPkgs, stalkList);
if(diff != null)
{
stalkList.removeAll(diff);
}
}
}
}, 20000, 6000); // every 6 seconds
return START_STICKY;
}
private RunningAppProcessInfo getForegroundApp() {
RunningAppProcessInfo result = null, info = null;
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List <RunningAppProcessInfo> l = activityManager.getRunningAppProcesses();
Iterator <RunningAppProcessInfo> i = l.iterator();
while(i.hasNext()) {
info = i.next();
if(info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND
&& !isRunningService(info.processName)) {
result = info;
break;
}
}
return result;
}
private boolean isRunningService(String processName) {
if(processName == null)
return false;
RunningServiceInfo service;
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List <RunningServiceInfo> l = activityManager.getRunningServices(9999);
Iterator <RunningServiceInfo> i = l.iterator();
while(i.hasNext()){
service = i.next();
if(service.process.equals(processName))
return true;
}
return false;
}
private boolean isRunningApp(String processName) {
if(processName == null)
return false;
RunningAppProcessInfo app;
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List <RunningAppProcessInfo> l = activityManager.getRunningAppProcesses();
Iterator <RunningAppProcessInfo> i = l.iterator();
while(i.hasNext()){
app = i.next();
if(app.processName.equals(processName) && app.importance != RunningAppProcessInfo.IMPORTANCE_SERVICE)
return true;
}
return false;
}
private boolean checkifThisIsActive(RunningAppProcessInfo target){
boolean result = false;
ActivityManager.RunningTaskInfo info;
if(target == null)
return false;
final ActivityManager activityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List <ActivityManager.RunningTaskInfo> l = activityManager.getRunningTasks(9999);
Iterator <ActivityManager.RunningTaskInfo> i = l.iterator();
while(i.hasNext()){
info=i.next();
if(info.baseActivity.getPackageName().equals(target.processName)) {
result = true;
break;
}
}
return result;
}
// what is in b that is not in a ?
public static Collection subtractSets(Collection a, Collection b)
{
Collection result = new ArrayList(b);
result.removeAll(a);
return result;
}

Reno
- 33,594
- 11
- 89
- 102
-
where do i write the above code; in the service or in d activity which calls the service – jehan Nov 09 '11 at 08:32
-
What is subtractSets ? Can't find it or do you have to write it yourself? – Sunkas Nov 30 '12 at 10:32
-
@Jonas It's a simple collection diff. This code is pretty old - I've refactored it since, for now I've added some helper functions. When I get the time I'll retest and update. – Reno Dec 01 '12 at 08:01
-
ok! Do you know what the difference is between getRunningAppProcesses() and getRunningTasks() ? I our project at work we do not want to use getRunningTasks since it is not allowed (recommended?) by Google (see http://developer.android.com/reference/android/app/ActivityManager.html#getRunningTasks(int) ). Is it enough to use getRunningAppProcesses to determine what apps are running? – Sunkas Dec 03 '12 at 08:30
-
To know the difference between tasks and process [please read this](http://stackoverflow.com/questions/4936581/difference-between-task-and-process-in-android). I see, those warnings weren't on the dev page when I used these APIs. – Reno Dec 03 '12 at 11:06
1
You can't get a broadcast when someone starts any other app. The only thing you can do is start a service that polls and periodically checks if new apps have been started (using Reno's code above for example).
Also if you start this service when the phone starts you have a reasonably good solution.

Sunkas
- 9,542
- 6
- 62
- 102