简体   繁体   English

Android上的启动画面无法正常工作

[英]Splash Screen on Android not working

I'm developing an Android app. 我正在开发一款Android应用。 The app should show a Splash Screen at startup while checking if a file is updated. 应用程序应在启动时显示启动画面,同时检查文件是否已更新。 If the file is not updated, it launches an Async Task to update the file. 如果文件未更新,则会启动异步任务以更新文件。 The problem is, the image of the Splash Screen only shows when the file actually needs updating. 问题是,启动画面的图像仅显示文件实际需要更新的时间。 Else, a black screen shows while performing the check. 否则,执行检查时会显示黑屏。

My SplashScreen activity: 我的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 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>

You are killing your Splash before it be on the Screen. 你在飞溅之前就杀了他的Splash。 Because you are killing it inside the onResume method. 因为你在onResume方法中杀了它。 Look this piece of documentation: 看看这篇文档:

The foreground lifetime of an activity happens between a call to onResume() until a corresponding call to onPause(). 活动的前景生命周期发生在对onResume()的调用之间,直到对onPause()的相应调用。 During this time the activity is in front of all other activities and interacting with the user. 在此期间,活动位于所有其他活动之前并与用户交互。 An activity can frequently go between the resumed and paused states -- for example when the device goes to sleep, when an activity result is delivered, when a new intent is delivered -- so the code in these methods should be fairly lightweight. 活动可以经常在恢复和暂停状态之间进行 - 例如,当设备进入休眠状态,交付活动结果时,何时传递新意图 - 因此这些方法中的代码应该相当轻量级。

You can use a handler, it is more elegant than Thread to solve this problem, but the main ideia is the same of @sheetal. 你可以使用一个处理程序,它比Thread更优雅来解决这个问题,但主要的ideia与@sheetal相同。

On onCreate method: on onCreate方法:

 handler = new Handler();

On onResume method: on onResume方法:

   handler.postDelayed(new Runnable() {
    public void run() {
      if(!isFileUpdated()){
        new PlacesService(this).execute();
      }else{
        intentAndFinish();
      }
    }
}, 1000);

If your file is updated You are closing your activity in onResume and switching to main activity and the process will take fraction of milliseconds to do that..so it is very much possible you will never see your spash screen..If you want you can use 如果您的文件已更新您正在关闭onResume中的活动并切换到主要活动,并且该过程将花费几分之一毫秒才能完成。因此,您很可能永远不会看到您的spash屏幕。如果您想要,可以采用

  private void intentAndFinish() {
     Thread.sleep(10000)
    finish();
    Intent mainIntent = new Intent(this, MainActivity.class);
    startActivity(mainIntent);  
}

Just to catch up with your splashScreen. 只是为了赶上你的splashScreen。

See @bruno 's post...We should use a handler 看@bruno的帖子......我们应该使用一个处理程序

You call to isFileUpdated() takes time put it in background You asyntask should be something like this 你调用isFileUpdated()需要时间把它放在后台你asyntask应该是这样的

    public class PlacesService extends Asynctask<Void,Void,Void>{
    boolean flag=false
    public Void doInBackground(){
    flag=isFileUpdated()
    if (!flag){
    return
    }else {
    // Do your file update
    }
    return 

    }

public onPostExcecute(){
intentAndFinish();

}
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM