Android——倒计时跳转+sharedpreferences

public class MainActivity extends Activity {
    // 3秒钟后,从图1跳转到图2(10)
    private Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg) {
            int what=msg.what+1;
            if (what<=2) {
                tv.setText("倒计时:"+(3-what)+"秒");
                if (what==2) {
                    // 使用SharedPreferences保存状态,使应用第二次进入,进入图1,3秒后跳转到图2,不再播放动画(10)
                    SharedPreferences sp=getSharedPreferences("info", MODE_PRIVATE);
                    sp.edit().putBoolean("frist",true).commit();
                    startActivity(new Intent(MainActivity.this,Main2Activity.class));
                    finish();
                }
            }
        };
    };
    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        
        new Thread(){
            
            public void run() {
                
                
                //题目要求3秒
                for (int i = 0; i <3; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    //没一秒都要发送
                    handler.sendEmptyMessage(i);
                    
                }
            };
        }.start();
        // 使用SharedPreferences保存状态,使应用第二次进入,进入图1,3秒后跳转到图2,不再播放动画(10)
        SharedPreferences spw=getSharedPreferences("info", MODE_PRIVATE);
        boolean boolean1 = spw.getBoolean("frist", false);
        if (boolean1) {
            startActivity(new Intent(MainActivity.this,Main2Activity.class));
            finish();
        }
        
        
    }
原文地址:https://www.cnblogs.com/leshen/p/7363984.html