Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:49:43+00:00 2026-06-17T21:49:43+00:00

As far as I know, there is no system API for me to get

  • 0

As far as I know, there is no system API for me to get user idle time. When I say user idle time, I mean user have some interaction on the touch screen within my app. Therefore, I want to track it by my self. The way come up to my mind is to extends Activity and override onuserinteraction method to save the last user active time.

But the challenge is that my app have multiple processes. I am not sure the following way is the correct and efficient way.

I want to use SharedPreference with MODE_WORLD_WRITEABLE flag to store the user last active time. To avoid the performance issue, I also cache the last active time within the activity in each activity. And I only save the new time to SharedPrefernces if the diff time > 1 second.

Is this way efficient compared to using aidl? Actually, is aidl also share variable using file? If yes, I think the two ways should have similar performance, right? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:49:44+00:00Added an answer on June 17, 2026 at 9:49 pm

    Instead writing it down every time, from everywhere, make this a global function in your App:

    public class MyApp extends Application {
        private static SharedPreferences sPreference;
    
        private static final long MIN_SAVE_TIME = 1000;
        private static final String PREF_KEY_LAST_ACTIVE = "last_active";
        private static final String PREF_ID_TIME_TRACK = "time_track";
    
        public static void saveTimeStamp(){
            if(getElapsedTime() > MIN_SAVE_TIME){
                sPreference.edit().putLong(PREF_KEY_LAST_ACTIVE, timeNow()).commit();
            }
        }
    
        public static long getElapsedTime(){
            return timeNow() - sPreference.getLong(PREF_KEY_LAST_ACTIVE,0);
        }
    
        private static long timeNow(){
            return Calendar.getInstance().getTimeInMillis();
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            sPreference = getSharedPreferences(PREF_ID_TIME_TRACK,MODE_PRIVATE);
        }
    }
    

    Add Application class to manifest: <application android:name="com.example.MyApp"

    Place saving functionality in an abstract Activity class:

    public abstract class TimedActivity extends Activity {
    
        @Override
        public void onUserInteraction() {
            super.onUserInteraction();
            MyApp.saveTimeStamp();
        }
    
        public long getElapsed(){
            return MyApp.getElapsedTime();
        }
    
    }
    

    Now, extend all your activities from this class, all of them will be auto-save time, and will be able to use getElapsed().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As far as I know there's some kind of Linux in the Chrome OS
As far as I know, there's no way to get url hash in a
I know there are several similar questions on SO, I have used some of
As far as I know there is no way to migrate existing Facebook comments
as far as i know there are cross domain restictions that wont allow you
As far as I know there is no way to do this, but I
I am working on a Metro app, and as far as I know there
As far as i know, there is no direct equivalent in C#. My current
As far as I know, there is currently no JAXB implementation which would work
I am implementing a simple license-file system, and would like to know if there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.