我有我的第一节课,它是一个加载屏幕。

 public class Loading extends Activity {

public int switchscreensvalue = 0;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loadinglayout);

    new Loadsounds().execute(switchscreensvalue);


            if (switchscreensvalue == 1)
            {
                Intent myIntent = new Intent(Loading.this, main.class);
                startActivityForResult(myIntent, 0);
            }



        }
}

然后我有了异步任务类。
    public class Loadsounds extends AsyncTask<Integer, Void, Void> {

@Override
protected Void doInBackground(Integer... params) {

        SoundManager.addSound(0, R.raw.rubber);
    SoundManager.addSound(1, R.raw.metal);
    SoundManager.addSound(2, R.raw.ice);
    SoundManager.addSound(3, R.raw.wind);
    SoundManager.addSound(4, R.raw.fire);

    return null;
}


protected void onPostExecute(Integer...params){
    int switchscreensvalue = 1;
}

 }

我希望它启动asynctask,将5个声音加载到一个声卡中,完成后,将int“switchscreensvalue”更改为1。然后,当“switchScreensValue”=1时,加载屏幕应更改为主屏幕。但它不起作用。有谁能帮我,只是第一次学习异步任务。事实上,对于Java来说还是相当新的。我需要异步任务加载5个声音,然后将活动从加载更改为主活动。

最佳答案

这应该管用…

public class Loading extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loadinglayout);

        new Loadsounds().execute();
    }

    public void startMainActivity() {
        Intent myIntent = new Intent(this, Main.class);
        startActivityForResult(myIntent, 0);
    }

    private class Loadsounds extends AsyncTask<Void, Void, Boolean> {
        boolean success = false;

        @Override
        protected Void doInBackground(Void... params) {
            // Load sounds here and set success = true if successful
        }
        return success;

        @Override
        protected void onPostExecute(Boolean...result) {
            if (result)
                startMainActivity();
        }
    }
)

07-24 09:49