0

私は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>
4

3 に答える 3

2

スプラッシュが画面に表示される前に殺しています。onResume メソッド内でそれを殺しているからです。このドキュメントを見てください:

アクティビティのフォアグラウンド ライフタイムは、onResume() の呼び出しから対応する onPause() の呼び出しまでの間に発生します。この間、アクティビティは他のすべてのアクティビティの前にあり、ユーザーと対話します。アクティビティは頻繁に再開状態と一時停止状態の間を行き来することがあります。たとえば、デバイスがスリープ状態になったとき、アクティビティ結果が配信されたとき、新しいインテントが配信されたときなどです。したがって、これらのメソッドのコードはかなり軽量である必要があります。

ハンドラーを使用できます。この問題を解決するには、スレッドよりもエレガントですが、主なアイデアは @sheetal と同じです。

onCreate メソッド:

 handler = new Handler();

onResume メソッド:

   handler.postDelayed(new Runnable() {
    public void run() {
      if(!isFileUpdated()){
        new PlacesService(this).execute();
      }else{
        intentAndFinish();
      }
    }
}, 1000);
于 2013-03-18T14:57:11.673 に答える
0

ファイルが更新された場合onResumeでアクティビティを閉じ、メインアクティビティに切り替えると、プロセスに数ミリ秒かかります。そのため、spash画面が表示されない可能性が非常に高くなります。使用する

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

スプラッシュスクリーンに追いつくためだけに。

@brunoの投稿を参照してください...ハンドラーを使用する必要があります

于 2013-03-18T14:47:02.007 に答える
0

isFileUpdated() を呼び出すと、バックグラウンドで時間がかかります ayntask は次のようになります

    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();

}
    }
于 2013-03-18T15:19:27.707 に答える