繁体   English   中英

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

[英]Splash Screen on Android not working

我正在开发一款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>

你在飞溅之前就杀了他的Splash。 因为你在onResume方法中杀了它。 看看这篇文档:

活动的前景生命周期发生在对onResume()的调用之间,直到对onPause()的相应调用。 在此期间,活动位于所有其他活动之前并与用户交互。 活动可以经常在恢复和暂停状态之间进行 - 例如,当设备进入休眠状态,交付活动结果时,何时传递新意图 - 因此这些方法中的代码应该相当轻量级。

你可以使用一个处理程序,它比Thread更优雅来解决这个问题,但主要的ideia与@sheetal相同。

on onCreate方法:

 handler = new Handler();

on onResume方法:

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

如果您的文件已更新您正在关闭onResume中的活动并切换到主要活动,并且该过程将花费几分之一毫秒才能完成。因此,您很可能永远不会看到您的spash屏幕。如果您想要,可以采用

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

只是为了赶上你的splashScreen。

看@bruno的帖子......我们应该使用一个处理程序

你调用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