0

我想在启动屏幕之间进行调整,首先在我的应用程序上显示 asynchtask,我从 splash 5 seconde 开始,然后是 asynchtask,我希望当我启动我的启动画面时 asynchtask 也开始加载,这是我的启动代码:

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

        }
    }
4

2 回答 2

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

    }
}
于 2013-05-06T16:54:21.113 回答
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();

    }
 }
}
于 2013-05-06T16:51:23.720 回答