我想在某些活动的启动画面上为应用的API加载数据。
所以我从该活动中删除了异步任务,并在启动画面中写入。
我希望启动画面应该在handler.i.e SPLASH_TIME_OUT
中提供的同时加载,并且导航数据应该在着陆活动中加载。
如果在提出问题时有任何错误,请纠正我。
基本上想要在启动画面上加载数据而不会花费更多时间。
这是代码段。
new Handler().postDelayed(new Runnable() {
public void run() {
// After 5 seconds redirect to another intent
if(CommonUtils.getSavedPreferences(getApplicationContext(), Constants.ACCESS_TOKEN).length()>0)
{
Intent i=new Intent(SplashActivity.this,DashBoardActivity.class);
SplashAsync async=new SplashAsync();
async.execute("");
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}else{
Intent i=new Intent(SplashActivity.this,LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
//Remove activity
// finish();
}
}, SPLASH_TIME_OUT); //it is set to 3000
异步任务
public class SplashAsync extends AsyncTask<String, Void, String>{
private Dialog mDialog;
private String response = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
mDialog = new Dialog(SplashActivity.this, android.R.style.Theme_Translucent);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setContentView(R.layout.loading);
mDialog.setCancelable(true);
mDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
response = callForDirectoryRequest();
System.out.println(response);
return response;
}
@Override
protected void onPostExecute(String result) {
try{
super.onPostExecute(result);
if (mDialog != null) {
mDialog.dismiss();
}
Gson gson = new Gson();
mMemberResponseParser = gson.fromJson(response,
new TypeToken<List<MemberResponseParser>>(){}.getType());
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private String callForDirectoryRequest() {
String url = Constants.DIRECTORY_URL+"member_email="+CommonUtils.getSavedPreferences(getApplicationContext(), Constants.MEMBER_EMAIL)+"&member_token="+CommonUtils.getSavedPreferences(getApplicationContext(), Constants.ACCESS_TOKEN);
Log.e("", "URL IS NOW " + url);
try {
@SuppressWarnings("deprecation")
StringEntity stringEntity = new StringEntity("");
ServiceCall mServiceCall=new ServiceCall(getApplicationContext(), url, stringEntity);
response = mServiceCall.getHttpGetServiceResponse();
System.out.println("################### Response:"+ response);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}
有人可以为此提供帮助吗?