How to call another activity after certain time limit

后端 未结 4 1525
遥遥无期
遥遥无期 2021-02-06 03:09

How to give time limit for calling one activity to another activity. I want to call another activity (Ex calling A class to B class) by given some time limit. I used alarmManage

相关标签:
4条回答
  • 2021-02-06 03:39

    Here is a simply solution. this could be used for example for showing a splash activity for 1 second then going into the main app:

    public class Splash extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.splash);
    
                int secondsDelayed = 1;
                new Handler().postDelayed(new Runnable() {
                        public void run() {
                                startActivity(new Intent(Splash.this, ActivityB.class));
                                finish();
                        }
                }, secondsDelayed * 1000);
        }
    }
    
    0 讨论(0)
  • 2021-02-06 03:39

    Let's hope following example may help.

    Here I have call MainActivity.java after certain time (2 seconds in this example) from MainPage.java

    CODE:

    public class MainPage extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main_page);
            Handler handler=new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(MainPage.this,MainActivity.class));
                }
            },2000L);
        }
    
    }
    
    0 讨论(0)
  • 2021-02-06 03:52

    This can also be done using android CountDownTimer class.

    See this example for a 3 second delay.

    new CountDownTimer(3000, 1000) {
        public void onFinish() {
             Intent startActivity = new Intent(ThisActivity.this,ActivityToStart.class);
             startActivity(startActivity);
            finish();
        }
    
        public void onTick(long millisUntilFinished) {
        }
    
    }.start();
    

    You may also need to define your parent activity in AndroidManifest.xml file,

    <activity
          android:name=".ActivityToStart"
          android:label="Back"
          android:parentActivityName=".MainActivity" >
    
          <!-- Parent activity meta-data to support 4.0 and lower -->
          <meta-data
               android:name="android.support.PARENT_ACTIVITY"
               android:value=".MainActivity" />
    </activity>
    
    0 讨论(0)
  • 2021-02-06 03:54

    You could use a Timer and add a TimerTask that is executed after a specific delay.

    Here is a more or less completed example:

    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
    
       public void run() {
    
          //here you can start your Activity B.
    
       }
    
    }, 10000);
    

    The example above executes a new TimerTask in 10 seconds. Inside the TimerTask you can override the run method. In the run method you can start your new activity. The run method is executed after the delay. In this example it is 10'000 milliseconds.

    0 讨论(0)
提交回复
热议问题