引导页之倒计时跳转 <并记录状态>

来源:互联网 发布:网上体彩软件下载 编辑:程序博客网 时间:2024/06/05 02:12
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/text_1"        /></LinearLayout>

代码实现

public class hone extends Activity {    private TextView textView;    int time=3;    Handler handler=new Handler(){        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            if (time==0){                Intent intent = new Intent(MainActivity.this, HomeActivity.class);                startActivity(intent);                finish();            }else {                textView.setText(time+": 秒跳转");            }        }    };    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_aaa);        textView = (TextView) findViewById(R.id.text_1);    //getSharedPreferences 记录状态        SharedPreferences sh = getSharedPreferences("name", MODE_PRIVATE);        SharedPreferences.Editor edit = sh.edit();        boolean aTrue = sh.getBoolean("true", false);        if (aTrue){            Intent intent = new Intent(MainActivity.this, HomeActivity.class);            startActivity(intent);            finish();        }else {            edit.putBoolean("true",true);            edit.commit();            Timer timer = new Timer();            timer.scheduleAtFixedRate(new TimerTask() {                @Override                public void run() {                    time--;                    handler.sendEmptyMessage(0);                }            }, 0, 2000);        }    }}