我正在开发一个 Android 应用程序。该应用程序应在启动时显示启动画面,同时检查文件是否已更新。如果文件没有更新,它会启动一个异步任务来更新文件。问题是,启动画面的图像只显示文件实际需要更新的时间。否则,在执行检查时会显示黑屏。
我的 SplashScreen 活动:
public class SplashActivity extends Activity
{
private final static String placesFile = "places";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_splash);
}
@Override
protected void onResume()
{
super.onResume();
if(!isFileUpdated()){
new PlacesService(this).execute();
}else{
intentAndFinish();
}
}
private void intentAndFinish() {
finish();
Intent mainIntent = new Intent(this, MainActivity.class);
startActivity(mainIntent);
}
/**
*
* @return false if Places Data is too old
*/
private boolean isFileUpdated() {
int daysOld = 0;
File f = new File(this.getFilesDir().getAbsolutePath() +"/"+placesFile);
if(f.exists()){
System.out.println("existe");
}
Date d = new Date();
Date currentDate = new Date(System.currentTimeMillis());
d.setTime(f.lastModified());
if(currentDate.compareTo(d)>0)
daysOld = determineDifferenceInDays(d, currentDate);
return daysOld < Consts.PLACES_DAYS_OLD_QTY_PERMITTED?true:false;
}
private static int determineDifferenceInDays(Date date1, Date date2) {
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
long diffInMillis = calendar2.getTimeInMillis() - calendar1.getTimeInMillis();
return (int) (diffInMillis / (24* 1000 * 60 * 60));
}
public void onResultFromAsyncTask(boolean finished) {
if(finished){
intentAndFinish();
}
}
}
activity_splash.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="@drawable/splash_es"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>