如何在进入第二项活动后停止这段时间?

时间:2018-01-26 15:43:55

标签: java android

我有用于自动点击按钮的java代码来打开新活动,但是在新活动打开后,此时间不会停止,因此活动1返回并再次运行到自动点击按钮。 我使用下面的代码

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_adsbonus );
    btnN=(Button)findViewById( R.id.btnNext );
    btnR=(Button) findViewById( R.id.btnRe );
    btnR.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent (ActivityAds.this, ActivityStarquiz.class);
            startActivity( i );
        }
    } );
    viewBinds();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            btnR.performClick();

        }
    }, 35000);

我试试这个java代码,但不能用于kill:

public void onBackPressed(){
    super.onBackPressed();
    Intent i = new Intent (this, ActivityStarquiz.class);
    i.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
    startActivity( i );
    finish();
    finishAffinity();
    System.exit( 0 );

1 个答案:

答案 0 :(得分:0)

你可以这样停止时间:

//in your activity
Runnable mRunable;
Handler mHandler;

//in on create
mHandler = new Handler();
mRunable = new Runnable()
{
    @Override
    public void run() 
    {
        btnR.performClick();
    }
};
mHandler.postDelayed(mRunnable,35000);

//and when you want to stop
mHandler.removeCallbacks(mRunnable);