首页 > 解决方案 > 如何在用户可以导航回活动的时间间隔后切换活动?

问题描述

我有两个活动;一个是IntermediateActivity,一个是ButtonPress活动。有IntermediateActivity一个 5 秒计时器。计时器结束后,ButtonPress活动打开,其中用户必须在一段时间内(现在为 2 秒)按下放置在屏幕上随机位置的按钮,然后我开始一个意图,IntermediateActivity如果用户可以按下按钮或时间用完。

正在发生的事情是,对于 的运行ButtonPressIntermediateActivity它会自行打开,有时,计时器上的计时器IntermediateActivity未完成,但ButtonPress活动已启动。添加计时器ButtonPress导致这两个活动的行为非常不稳定。

以下是我根据计时器和用户操作IntermediateActivity在Activity 中调用意图的方式:ButtonPress

public class ButtonPress extends AppCompatActivity implements View.OnClickListener {

    private Button button;
    private DisplayMetrics displaymetrics;
    private RelativeLayout loOPenArea;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.button_press);

        button = findViewById(R.id.my_button);
        button.setClickable(true);
        button.setOnClickListener(this);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent i = new Intent(ButtonPress.this, IntermediateActivity.class);
                startActivity(i);
                finish();
            }
        }, 2000);


        // Wait until the first layout to get button size and placement to compute x/y placement range.
        button.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                button.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                // Capture width and x/y placement ranges here
                final Button button = (Button) findViewById(R.id.my_button);
                displaymetrics = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
                loOPenArea = (RelativeLayout) findViewById(R.id.open_area_press);

                // Button will have a horizontal margin from zero to the width of its parent less
                // the width of the button less the initial left side of the button which is likely
                // to be the initial margin.
                int buttonMaxXMargin = loOPenArea.getWidth() - button.getWidth() - button.getLeft();
                // Similar for the vertical placement.
                int buttonMaxYMargin = loOPenArea.getHeight() - button.getHeight() - button.getTop();

                int leftMargin = new Random().nextInt(buttonMaxXMargin);
                int topMargin = new Random().nextInt(buttonMaxYMargin);

                ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) button.getLayoutParams();
                p.setMargins(leftMargin, topMargin, 0, 0);

                button.setLayoutParams(p);
            }
        });

    }

    @Override
    public void onClick(View v) {
        Intent i = null;
        switch (v.getId()) {

            case R.id.my_button:
                i = new Intent(ButtonPress.this, IntermediateActivity.class);
                startActivity(i);
                finish();
                break;
        }
    }



}

我尝试了多种方法,例如使用该finish()函数并android:finishOnTaskLaunch="true"在清单文件中添加这一行,但它仍然无法正常工作。

标签: androidandroid-intentandroid-activitytimercountdown

解决方案


推荐阅读