Asynctask与启动画面

时间:2013-05-06 16:43:46

标签: android android-asynctask

我想在启动画面之间使用asynchtask在我的应用程序中首先显示,我开始使用splash 5 seconde,然后是asynchtask,我希望当我启动我的启动时asynchtask开始加载,这是我的启动代码:< / p>

public class SplashActivity extends Activity {

    private static String Spsc = SplashActivity.class.getName();
    private static long time = 5;   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  // Removes notification bar
        setContentView(R.layout.splash);
        IntentLauncher launcher = new IntentLauncher();
        launcher.start();
    }
    private class IntentLauncher extends Thread {
        @Override
        public void run() {
            try {
                Thread.sleep(time*1000);
            } catch (Exception e) {
                Log.e(Spsc, e.getMessage());
            }
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            SplashActivity.this.startActivity(intent);
            SplashActivity.this.finish();
        }
    }
}

这是我从mainActivity的启发:

class FetchPosts extends AsyncTask<Void, Void, Void> {
        private ProgressDialog progressDialog;
        @Override
        protected void onPreExecute() {         
            super.onPreExecute();
            progressDialog = ProgressDialog.show(MainActivity.this, "", getString(R.string.loading_message));
        }

        @Override
        protected Void doInBackground(Void... params) {
            articles = Services.getPosts(MainActivity.this);

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            progressDialog.dismiss();

        }
    }

2 个答案:

答案 0 :(得分:0)

简单

 public class SplashActivity extends Activity {

private static String Spsc = SplashActivity.class.getName();
private static long time = 5;   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  // Removes notification bar
    setContentView(R.layout.splash);
    IntentLauncher launcher = new IntentLauncher();
    launcher.start();
}
private class IntentLauncher extends Thread {
    @Override
    public void run() {
        try {
            Thread.sleep(time*1000);
        } catch (Exception e) {
            Log.e(Spsc, e.getMessage());
        }
        //Intent intent = new Intent(SplashActivity.this, MainActivity.class);
        //SplashActivity.this.startActivity(intent);
        //SplashActivity.this.finish();
          new FetchPosts().execute();
    }
   }
public class FetchPosts extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {         
        super.onPreExecute();
        progressDialog = ProgressDialog.show(MainActivity.this, "", getString(R.string.loading_message));
    }

    @Override
    protected Void doInBackground(Void... params) {
        articles = Services.getPosts(MainActivity.this);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        progressDialog.dismiss();

    }
 }
}

答案 1 :(得分:0)

尝试以下代码,它将在启动启动画面活动时启动asynctask

public class SplashActivity extends Activity {

private static String Spsc = SplashActivity.class.getName();
private static long time = 5;   
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);  // Removes notification bar
    setContentView(R.layout.splash);
    FetchPosts obj=new FetchPosts();
    obj.execute("dummystring");
 }
}

class FetchPosts extends AsyncTask<String, Void, String> {
    private ProgressDialog progressDialog;
    @Override
    protected void onPreExecute() {         
        super.onPreExecute();
        progressDialog = ProgressDialog.show(MainActivity.this, "", getString(R.string.loading_message));
    }

    @Override
    protected Void doInBackground(String... params) {
        articles = Services.getPosts(MainActivity.this);

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        progressDialog.dismiss();

    }
}