Application Launch Count

Application Launch Count

This is actually quite simple. Using SharedPreference or the Database.

during OnCreate add 1 to the numberofTimes counter and commit.

OnCreate (Bundle bundle){
mPref = getPreferences();
int c = mPref.getInt("numRun",0);
c++;
mPref.edit().putInt("numRun",c).commit();
//do other stuff...
}

OnCreate is called regardless of you start the app or you resume the app, but isFinishing() returns true if and only iff the user (or you) called finish() on the app (and it was not being destroyed by the manager)

This way you only increment when you are doing fresh start.

the isFinishing() Method inside of a OnPause method to check to see if the activity is being finish() or just being paused.

@Override
protected void OnPause(){
if(!isFinishing()){
c = mPref.getInt("numRun",0);
c--;
mPref.edit().putInt("numRun",c).commit();
}
//Other pause stuff.
}

This covers all your scenarios:

1. user starts app/activity (+1)-> finishes app, exit with finish()
2. user starts app (+1) -> pause (-1) -> returns (+1)-> finish
3. user starts app (+1) -> pause (-1) -> android kills process (0) -> user returns to app (+1) -> user finish.

every scenario you only increment the "times run" counter once per "run" of the activity

Android: count number of app launches

First off, thank you all for your answers. They all helped some way or another.

That said, after several tries and different approaches, I think there is no actual 100% reliable way of determining when the user has launched the app. None of the activity life-cycle callbacks can really be used tackle the issue, neither individually nor combined.

The closest I got was counting the number of onResume() calls, and then substracting from it in onPause() depending on the return of isFinishing(). This approach, however, doesn't account for the home button, among maybe other things a user can do to hide apps.

I will update this answer if I ever find a way to do this reliably.

Count number of times app has been launched using Swift?

Add this in AppDelegate in applicationDidFinishLaunching method.

Swift 3 and Swift 4:

// get current number of times app has been launched
let currentCount = UserDefaults.standard.integer(forKey: "launchCount")

// increment received number by one
UserDefaults.standard.set(currentCount+1, forKey:"launchCount")

Swift 2:

// get current number of times app has been launched
let currentCount = NSUserDefaults.standardUserDefaults().integerForKey("launchCount")

// increment received number by one
NSUserDefaults.standardUserDefaults().setInteger(currentCount+1, forKey:"launchCount")

According to documentation there's no more need to call:

UserDefaults.standard.synchronize()

Waits for any pending asynchronous updates to the defaults database and returns; this method is unnecessary and shouldn't be used.

(Android) detect when another app is launched / launch count of all app

you can get the UsesStats of the top foreground Applications. but using this the user have rights to Accept or Decline for each app separately.
getting the permission

Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);

and this will return the foreground applications..

    String topPackageName ;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager mUsageStatsManager = (UsageStatsManager)getSystemService("usagestats");
long time = System.currentTimeMillis();
// We get usage stats for the last 10 seconds
List<UsageStats> stats = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000*10, time);
// Sort the stats by the last time used
if(stats != null) {
SortedMap<Long,UsageStats> mySortedMap = new TreeMap<Long,UsageStats>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(),usageStats);
}
if(!mySortedMap.isEmpty()) {
topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
}

How should I count the number of launches of an application in iPhone

use NSUserDefaults in applicationDidBecomeActive:.

NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    NSInteger appLaunchAmounts = [userDefaults integerForKey:@"LaunchAmounts"];
    if (appLaunchAmounts == 5)
    {
       //Use AlertView

    }
    [userDefaults setInteger:appLaunchAmounts+1 forKey:@"LaunchAmounts"];

Launch Count on iPhone App

Use NSUserDefaults:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger launchCount = [prefs integerForKey:@"launchCount"];
launchCount++;
NSLog(@"Application has been launched %d times", launchCount);
[prefs setInteger:launchCount forKey:@"launchCount"];


Related Topics



Leave a reply



Submit