安卓开发-app欢迎界面延迟转载
原创
©著作权归作者所有:来自51CTO博客作者不秃头的小李同学的原创作品,请联系作者获取转载授权,否则将追究法律责任
当开发安卓应用时我们想让别人看到我们的欢迎界面,直接跳转主界面略显突兀,且直接转载速度过快。因此大多采用延迟转载,以下分享一下延迟转载的方法。
(于你的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);
}
}