【问题标题】:How to measure Time between a start of an activity and stop?如何测量活动开始和停止之间的时间?
【发布时间】:2015-05-02 20:29:45
【问题描述】:

我认为也许它可以与 startactivityforresult 一起使用,但不知道如何..?在测量之后,我希望这次出现在另一个活动中。

到目前为止我已经尝试过:

在主要活动中:

 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);

}

}

在活动中,我想停止时间:

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();
    }


}

}

【问题讨论】:

    标签: java android performance time


    【解决方案1】:

    从您的代码中删除以下行

    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");
    

    希望对你有帮助。

    【讨论】:

    • 谢谢 :D 我想在点击 sec 按钮后回到主要活动。我不能这样吗?
    【解决方案2】:

    你设置了两次监听器;第二次它覆盖了第一个永远不会被调用的监听器。

    【讨论】:

    • 谢谢! :) ..到底在哪里?
    • 这里:sec.setOnClickListener(this);sec.setOnClickListener(new OnClickListener() {
    【解决方案3】:

    你可以这样做

    long startTime = System.nanoTime();
    
    // your activity start/stop
    
    long endTime = System.nanoTime(); // save this time in SharedPreference and get in second activity
    

    【讨论】:

    • 谢谢!如何使用共享偏好?
    • 您已经接受了某人的回答。如果是新问题,请提出新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多