user6614195
user6614195

Reputation:

How to know the last launch time of an app

Hi After doing lots of Search , I could not find the answer of a question, that seems somewhat simple.

I have multiple apps installed on device. Is there any way of finding the last launching date of all apps?

Upvotes: 2

Views: 1665

Answers (2)

Shaishav
Shaishav

Reputation: 5312

Based on your requirements from the comments above, I'm providing a method to share the launch times between a bunch of our apps using SharedPreferences.

Firstly, we need to allow our apps to share local storage so they can access preference data. For this, we use the sharedUserId property in the app manifest. This can be your own unique string and each of the apps that share the space must have this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="example.com.myapp"
    android:sharedUserId="string.user.id.here">

    ...

</manifest>

Secondly, we use the Application class to determine when the app comes to the foreground and when it does, write the timestamp in Context.CONTEXT_IGNORE_SECURITY mode to share between our apps:

public class MyApp extends Application implements ActivityLifecycleCallbacks {

    private boolean appInBg;
    private int TIMEOUT = 5000;
    private Handler mHandler = new Handler();

    @Override
    public void onActivityResumed() {
        if (appInBg) {
            writeToPref();
        } else {
            mHandler.cancelCallbacksAndMessages(null);
        }
    }

    @Override
    public void onActivityPaused() {
        mHanlder.postDelayed(new Runnable() {

            @Override
            public void run() {
                appInBg = true;
            }

        }, TIMEOUT);
    }

    ...


    private void writeToPref() {
         SharedPreferences prefs = myContext.getSharedPreferences("run_prefs", Context.CONTEXT_IGNORE_SECURITY);
         prefs.edit().putInt("last_run", System.currentTimeMillis()).apply();
    }
}

Here, we allow a buffer time of 5 seconds to switch between screens. This should be adequate in most cases.

Finally, we can read the written SharedPreference value as follows:

Context context = createPackageContext("example.com.myapp", 0);
SharedPreferences pref = context.getSharedPreferences("run_prefs", Context.CONTEXT_IGNORE_SECURITY);
int lastLaunch = pref.getString("last_run", System.currentTimeMillis());
// Similarly, for other apps.

Hope this solves your issue.

Upvotes: 0

Jules Hummelink
Jules Hummelink

Reputation: 674

You could put the time and date in an SharedPref when the app opens. Then, the next time you open the app the app reads the SharedPref and displays it.

Something like this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Calendar c = Calendar.getInstance();

    SharedPreferences sp = this.getPreferences(Context.MODE_PRIVATE);

    String lastLaunch = sp.getString("launch", "First launch!");

    SharedPreferences.Editor editor = sp.edit();
    editor.putString("launch", c.getTime().toString());
    editor.commit();
}

The String lastLaunch is the last time it launched! If it's the first time the string is: "First launch!"

I hope that i have helped you a little bit :)

Upvotes: 2

Related Questions