当开发安卓应用时我们想让别人看到我们的欢迎界面,直接跳转主界面略显突兀,且直接转载速度过快。因此大多采用延迟转载,以下分享一下延迟转载的方法。

于你的activity活动java代码中修改

第①步构建一个Handler对象
第②步,onCreate方法中该对象调用 sendMessageDelayed方法

完整代码呈现:

public class welcome extends AppCompatActivity {

@SuppressLint("HandlerLeak")
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//界面转载
Intent intent = new Intent(welcome.this,MainActivity.class);
startActivities(new Intent[]{intent}); //start跳转
finish();//结束欢迎界面活动
}
};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
ActionBar actionBar = getSupportActionBar(); //取消标题头actionbar
if (actionBar != null) {
actionBar.hide();
}
//延迟发送信息2000Ms即2秒
handler.sendMessageDelayed(Message.obtain(), 2000);
}

@Override
protected void onDestroy() {
super.onDestroy();
handler.removeCallbacksAndMessages(null);
}
}