计时器结束前当前的自动启动功能?

时间:2018-08-20 08:29:54

标签: android countdowntimer

我试图在计时器启动时开始一项活动,然后在10秒后计时器结束时再次开始当前活动。但是5秒钟后,当前活动将再次开始,并且在计时器结束后将调用oFinish()方法。 我没有在当前活动中的任何其他地方或计时器启动时正在启动的活动中启动crrent活动。 请帮助我理解这一点。谢谢

timer= new CountDownTimer (time, 1000) {

                    public void onTick(long millisUntilFinished) {
                        switch (activity_state){
                            case 1:
                                //this toast is being used to display time
                            Toast.makeText(getApplicationContext(),String.valueOf(millisUntilFinished/1000),Toast.LENGTH_SHORT).show();
                                if(millisUntilFinished==time){
                                    finish();
                                    startActivity(new Intent(SettingsActivity_2.this,OneYearActivity.class));}
                                break;
                            case 2:
                                if(millisUntilFinished==time)
                                {finish();startActivity(new Intent(SettingsActivity_2.this,TwoYearActivity.class));}
                                break;
                            case 3:
                                if(millisUntilFinished==time)
                                {finish();startActivity(new Intent(SettingsActivity_2.this,ThreeYearActivity.class));}
                                break;
                            case 4:
                                if(millisUntilFinished==time)
                                {finish();startActivity(new Intent(SettingsActivity_2.this,AllYearActivity.class));}
                                break;
                        }
                    }
                    public void onFinish() {
                        Toast.makeText(getApplicationContext(),"OnFinishCalled",Toast.LENGTH_SHORT).show();
                        startActivity ( new Intent ( SettingsActivity_2.this,SettingsActivity_2.class ));
                    }
                }.start();
  

OneYearActivity.java   此活动是从第一种情况开始的。

public class OneYearActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_one_year_kid);
    initializing_components();
    setSupportActionBar(toolbar);

}}

我将从this main screen转到名为{strong> SettingActivity_2 的this password screen,如果密码正确,则应该转到名为 OneYearActivity的this activity ,并在10秒钟后, SettingsActivity_2 应重新开始。但是5秒钟后, SettingsActivity_2 开始。

  

添加处理程序后开始新活动   我的处理程序代码

handler=new Handler();
 r=new Runnable() {
 @Override
  public void run() {
finish();
 }
 };
  

从OneYearActivity启动新的Activity以播放视频

        learning_songs.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent(OneYearActivity.this,PlayVideo.class);
        intent.putExtra("video_Id","vkRDOcma9Qk");
            startActivityForResult(intent,1);
        }
    });
  

从OneActivity开始播放YouTube视频的活动

public class PlayVideo extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play_video);

    YouTubePlayerView youtubePlayerView =    findViewById(R.id.youtube_player_view);
    getLifecycle().addObserver(youtubePlayerView);
VideoPlayer videoPlayer=new VideoPlayer();
    Intent intent=getIntent();

String video_Id=intent.getStringExtra("video_Id");
videoPlayer.play_Video(youtubePlayerView,video_Id);

}

我要做什么

  1. 开始设置2
  2. 点击启动OneYearActivity,显示10秒钟
  3. 如果用户按下learning_songs,它将播放新的活动,并且应在上一个活动中启动的计时器结束时结束

我得到的东西

  1. 开始设置2
  2. 点击启动OneYearActivity,显示10秒钟
  3. 如果用户按下learning_songs,它将播放新的活动,并且随着计时器的结束,它不会完成 PlayingVideo 活动

3 个答案:

答案 0 :(得分:0)

OneYearActivity.java 添加以下代码。

final Handler handler = new Handler();
final Runnable r = new Runnable() {
    public void run() {
        openSettingsActivity_2WithRequestCode();
    }
};

然后在onCreate()

中这样称呼它
handler.postDelayed(r, 10000);

更新1

如果重复编写代码,则为所有活动和从abstract(ParentActivity)具有通用代码的一个extendsAppCompactActivity创建一个extends ParentActivity的子类并在代码子类活动中使用。

更新2

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 try {
  super.onActivityResult(requestCode, resultCode, data);

  if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
       handler.postDelayed(r, 1);
  }else{
      finish();
  }
 } catch (Exception ex) {
  Toast.makeText(Activity.this, ex.toString(),
   Toast.LENGTH_SHORT).show();
 }
}

更新3

什么是onActivityResult

https://developer.android.com/training/basics/intents/result

注意:如果您需要任何帮助,请根据您的要求告诉我,我会更新代码。

答案 1 :(得分:0)

您的代码应如下所示:
在活动中,您正在切换状态:

switch (activity_state) {
        case 1:
            //this toast is being used to display time
            Toast.makeText(getApplicationContext(), String.valueOf(millisUntilFinished / 1000), Toast.LENGTH_SHORT).show();
            startActivity(new Intent(SettingsActivity_2.this, OneYearActivity.class));
            finish();
            break;
        case 2:
            startActivity(new Intent(SettingsActivity_2.this, TwoYearActivity.class));
            finish();
            break;
        case 3:
            startActivity(new Intent(SettingsActivity_2.this, ThreeYearActivity.class));
            finish();
            break;
        case 4:
            startActivity(new Intent(SettingsActivity_2.this, AllYearActivity.class));
            finish();
            break;

        default:
            break;
    }

您想在10秒后进行的每个下一个活动:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //display whatever you need

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

            finish();
        }
    }, 10000);
}

答案 2 :(得分:0)

在您的情况下,请执行以下操作:

1。从SettingsActivity_2启动OneYearActivity

startActivity(new Intent(this, OneYearActivity.class));

2。为OneYearActivity的结果启动PlayVideoActivity

private static final int PLAY_VIDEO_REQUEST_CODE = 1;

/*protected */ Runnable finishAction = new Runnable() {
        @Override
        public void run() {
            finishActivity(PLAY_VIDEO_REQUEST_CODE);
            finish();
        }
    };

/* in initialize method*/
    learning_songs.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent(OneYearActivity.this,PlayVideo.class);
            intent.putExtra("video_Id","vkRDOcma9Qk");
            startActivityForResult(intent, PLAY_VIDEO_REQUEST_CODE);
            toolbar.postDelayed(finishAction, 10000);
        }
    });

OR

您可以从PlayVideo活动开始

youtubePlayerView.postDelayed(finishAction, 10000);

投入结果

/*protected */ Runnable finishAction = new Runnable() {
        @Override
        public void run() {
            setResult(RESULT_OK);
            finish();
        }
    };

并在收到此代码后在onActivityResult中完成OneYearActivity

if (resultCode == PLAY_VIDEO_REQUEST_CODE) {
    finish();
}