How would you calculate the total time an activity has been displayed on the screen?


  1. How would you calculate the total time an activity has been displayed on the screen? It should be the total time it had been on screen, across its various launches, since it has been installed.
package com.prog.time;

import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

public class ActresumetimeActivity extends Activity {
    long i,j;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        j=System.currentTimeMillis();
        Log.d("Time in pause",""+j);
        Toast.makeText(this,"foreground time "+(j-i)/1000.0, 2000).show();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        i=System.currentTimeMillis();
        Log.d("Time in resume",""+i);
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
    }
  
  
}

Comments

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams

SCJP1.6 Question and Answers