有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何测量活动开始和停止之间的时间?

我想也许它可以与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();
    }


}

}


共 (3) 个答案

  1. # 1 楼答案

    您正在设置侦听器两次;第二次,它重写从未被调用的第一个侦听器

  2. # 2 楼答案

    你可以这样做

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

    从代码中删除以下行

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

    并在主屏幕上调用

    getIntent().getStringExtra("time");
    

    希望这对你有帮助