我有以下活动:
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
StringRequest req = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
}
});
CustomVolleyRequestQueue.getInstance(mCtx).addToRequestQueue(req);
}
}
在请求发送时显示SplashActivity
,并在收到响应时打开其他活动。没关系,除了我需要显示SplashActivity
至少4秒。那我接下来该怎么做:
如果从show节目开始活动开始4秒后得到响应显示MainActivity
否则等待?
答案 0 :(得分:0)
作为回应使用计时器
private final int DELAY = 4000;
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
timer.cancel();
}
}, DELAY);