2

I want to develop an android application where it counts the number of times I have opened another application. Say it should count a number of times I have opened Whatsapp or may be Facebook. How can I achieve this through another android application? How to observe the activity, behavior of other applications in android.

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
Kumara
  • 301
  • 1
  • 3
  • 10
  • 3
    use sharedPreference to count numbers of your application. – Sunny Dec 11 '16 at 01:48
  • You can't set a listener to check that, however you can check if a app is open so you can get the list of installed apps and then you can try this: http://stackoverflow.com/questions/22500959/detect-when-other-application-opened-or-launched you run this periodically if you detect a certain app you will know that the user launched the app in the past, and you can increment a value in your database – Tiago Oliveira Dec 11 '16 at 01:57

1 Answers1

0

first 1. create a db ( local android SQLite)

then 2. get a list of all the installed applications on the device.Use Android Package Manager with getInstalledApplications()

PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

for (ApplicationInfo packageInfo : packages) {
    Log.d("Installed Apps", "Installed package :" + packageInfo.packageName + " Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
  1. save package names and launcher activities in your db

  2. get list of recently launched apps and increment the counter of your db or what you like to do

     ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
     List<ActivityManager.RecentTaskInfo> recentTasks = activityManager.getRecentTasks(Integer.MAX_VALUE,ActivityManager.RECENT_WITH_EXCLUDED);
    
       for (int i = 0; i < recentTasks.size(); i++)
        {
    
            String LocalApp = recentTasks.get(i).baseIntent.toString();
            int indexPackageNameBegin = LocalApp.indexOf("cmp=")+4;
            int indexPackageNameEnd = LocalApp.indexOf("/", indexPackageNameBegin);
            String pckge = LocalApp.substring(indexPackageNameBegin, indexPackageNameEnd);
            Log.d("Executed app", "Application executed : " +pckge);
        }
    
Jaydeep chatrola
  • 2,423
  • 11
  • 17
Charuක
  • 12,953
  • 5
  • 50
  • 88