如何测量 activity 开始和停止之间的时间?
How to measure Time between a start of an activity and stop?
我想也许它可以与 startactivityforresult 一起使用,但不知道如何......?测量之后我希望这次出现在另一个 activity.
到目前为止我已经试过了:
主要activity:
public class MainScreen extends Activity implements OnClickListener {
Button btn_start;
TextView textview1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
btn_start = (Button) findViewById(R.id.btn_start);
btn_start.setOnClickListener(this);
textview1 = (TextView) findViewById(R.id.textView1);
textview1.setText(getIntent().getStringExtra("time"));
}
@Override
public void onClick(View v) {
startActivityForResult(new Intent(this,Game.class), 0);
}
}
并且在 activity,我想在其中停止时间:
public class Game extends Activity implements OnClickListener{
Button start_time;
int i = 0;
TextView textview1;
Button RelativeLayout;
Button gameover;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout.setClickable(true);
setContentView(R.layout.activity_game);
start_time = (Button) findViewById(R.id.start_time);
start_time.setOnClickListener(this);
textview1 = (TextView) findViewById(R.id.textView1);
gameover = (Button) findViewById(R.id.gameover);
gameover.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
//Button b = (Button) findViewById(R.id.start_time);
Random r = new Random();
RelativeLayout decorView = (RelativeLayout) start_time.getParent();
int screenWidth = decorView.getWidth();
int screenHeight = decorView.getHeight();
long startTime = SystemClock.elapsedRealtime();
i++;
/*
Random r = new Random();
int x = r.nextInt(R.id.wrap_content);
int y = r.nextInt(R.id.wrap_content);
b.setX(x);
b.setY(y);
*/
if (i == 1 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 2 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 3 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 4 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 5 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 6 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
else if (i == 7) {
long difference = SystemClock.elapsedRealtime() - startTime;
Intent intent = new Intent(Game.this, MainScreen.class);
intent.putExtra("time",difference);
// Toast.makeText(getApplicationContext(), getIntent().getStringExtra("time"), Toast.LENGTH_LONG).show();
textview1.setText(getIntent().getStringExtra("time"));
finish();
}
}
}
你可以这样做
long startTime = System.nanoTime();
// your activity start/stop
long endTime = System.nanoTime(); // save this time in SharedPreference and get in second activity
您设置了两次侦听器;第二次它覆盖了第一个永远不会被调用的监听器。
从您的代码中删除以下行
sec.setOnClickListener(this);
并在 onClick(-)
startTime=System.currentTimeInMillis();
并且在
sec.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
totalTime=System.currentTimeInMillis()-startTime;
Intent intent = new Intent(Game.this, MainScreen.class);
intent.putExtra("time",totalTime);
startActivity(intent);
}
}
并在 MainScreen 调用
getIntent().getStringExtra("time");
希望对您有所帮助。
我想也许它可以与 startactivityforresult 一起使用,但不知道如何......?测量之后我希望这次出现在另一个 activity.
到目前为止我已经试过了:
主要activity:
public class MainScreen extends Activity implements OnClickListener {
Button btn_start;
TextView textview1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
btn_start = (Button) findViewById(R.id.btn_start);
btn_start.setOnClickListener(this);
textview1 = (TextView) findViewById(R.id.textView1);
textview1.setText(getIntent().getStringExtra("time"));
}
@Override
public void onClick(View v) {
startActivityForResult(new Intent(this,Game.class), 0);
}
}
并且在 activity,我想在其中停止时间:
public class Game extends Activity implements OnClickListener{
Button start_time;
int i = 0;
TextView textview1;
Button RelativeLayout;
Button gameover;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout.setClickable(true);
setContentView(R.layout.activity_game);
start_time = (Button) findViewById(R.id.start_time);
start_time.setOnClickListener(this);
textview1 = (TextView) findViewById(R.id.textView1);
gameover = (Button) findViewById(R.id.gameover);
gameover.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
//Button b = (Button) findViewById(R.id.start_time);
Random r = new Random();
RelativeLayout decorView = (RelativeLayout) start_time.getParent();
int screenWidth = decorView.getWidth();
int screenHeight = decorView.getHeight();
long startTime = SystemClock.elapsedRealtime();
i++;
/*
Random r = new Random();
int x = r.nextInt(R.id.wrap_content);
int y = r.nextInt(R.id.wrap_content);
b.setX(x);
b.setY(y);
*/
if (i == 1 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 2 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 3 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 4 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 5 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
if (i == 6 ) {
start_time.setX(r.nextInt(screenWidth - start_time.getWidth()));
start_time.setY(r.nextInt(screenHeight - start_time.getHeight()));
}
else if (i == 7) {
long difference = SystemClock.elapsedRealtime() - startTime;
Intent intent = new Intent(Game.this, MainScreen.class);
intent.putExtra("time",difference);
// Toast.makeText(getApplicationContext(), getIntent().getStringExtra("time"), Toast.LENGTH_LONG).show();
textview1.setText(getIntent().getStringExtra("time"));
finish();
}
}
}
你可以这样做
long startTime = System.nanoTime();
// your activity start/stop
long endTime = System.nanoTime(); // save this time in SharedPreference and get in second activity
您设置了两次侦听器;第二次它覆盖了第一个永远不会被调用的监听器。
从您的代码中删除以下行
sec.setOnClickListener(this);
并在 onClick(-)
startTime=System.currentTimeInMillis();
并且在
sec.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
totalTime=System.currentTimeInMillis()-startTime;
Intent intent = new Intent(Game.this, MainScreen.class);
intent.putExtra("time",totalTime);
startActivity(intent);
}
}
并在 MainScreen 调用
getIntent().getStringExtra("time");
希望对您有所帮助。