这个异步任务正确吗?

发布于 2024-12-25 02:11:27 字数 1247 浏览 0 评论 0原文

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

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



        }
}

然后我有我的 asynctask 类。

    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 来说仍然相当陌生。我需要 asynctask 加载 5 个声音,然后将活动从加载更改为主要活动。

I have my first class, its a loading screen.

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



        }
}

Then I have my asynctask class.

    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;
}

 }

I want it to start the asynctask, which loads 5 sounds into a soundboard, and when its done, change the int "switchscreensvalue" to 1. Then, the loading screen is supposed to change to the main screen when "switchscreensvalue" = 1. It does not work though. Please can anyone help me, just learning asynctasks for the first time. Still fairly new to Java in fact. I need the asynctask to load 5 sounds and then change the activity from loading to main activity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

脸赞 2025-01-01 02:11:27

那是因为你正在调用

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

onCreate() ,你不能保证它会再次被调用。

为什么不调用onPostExecute() 呢?

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

在设置变量后,

Thats because you are calling

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

in your onCreate() which you cant gaurantee will be called again.

Why dont you call

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

In your onPostExecute() after you set the variable as it should be.

顾忌 2025-01-01 02:11:27

这应该有效...

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

This should work...

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();
        }
    }
)
蛮可爱 2025-01-01 02:11:27

你就快到了...失去变量...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loadinglayout);
    new Loadsounds().execute(switchscreensvalue);
}


//                                       <Params, Progress, Result>
public class Loadsounds extends AsyncTask<Integer, Integer, Integer> {

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

    int result = 0;
    try {
        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);
        result = 1;
    } catch(Exception e){
        // some error handling if SoundManager.addSound throws exceptions?
    }

    return result; // positive integer on success
}


protected void onPostExecute(Integer result){
    if (!isCancelled() && (result != null) && (result > 0)
    {
        // int someResult = 0;
        Intent myIntent = new Intent(Loading.this, main.class);
        // should the following line be startActivity? 
        // difference between startActivity and startActivityForResult 
        // is that the latter returns an integer value where your zero is
        //startActivityForResult(myIntent, 0);
        startActivityForResult(myIntent, someResult);

        // Alternatively, just...
        // startActivity(myIntent);
    } else {
        // Log error, inform user, then close application?
    }

}

}

Your almost there... Lose the variable...

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.loadinglayout);
    new Loadsounds().execute(switchscreensvalue);
}


//                                       <Params, Progress, Result>
public class Loadsounds extends AsyncTask<Integer, Integer, Integer> {

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

    int result = 0;
    try {
        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);
        result = 1;
    } catch(Exception e){
        // some error handling if SoundManager.addSound throws exceptions?
    }

    return result; // positive integer on success
}


protected void onPostExecute(Integer result){
    if (!isCancelled() && (result != null) && (result > 0)
    {
        // int someResult = 0;
        Intent myIntent = new Intent(Loading.this, main.class);
        // should the following line be startActivity? 
        // difference between startActivity and startActivityForResult 
        // is that the latter returns an integer value where your zero is
        //startActivityForResult(myIntent, 0);
        startActivityForResult(myIntent, someResult);

        // Alternatively, just...
        // startActivity(myIntent);
    } else {
        // Log error, inform user, then close application?
    }

}

}
苍景流年 2025-01-01 02:11:27

我想你想说的是它没有切换活动?我听到你所说的范围,你确定这两个类都在同一个文件中吗?即,一个类(LoadingTask)位于另一个类(Loading 类)中?

尝试将“Loading.this”切换为“getApplication()”

我有

public class Loading extends Activity {
protected void onCreate(...){
    ... (As in above answer)
}

// LoadTask exists inside the Loading activity class
private class LoadTask extends AsyncTask<Integer, Integer, Integer>     {
    protected Integer doInBackground(Integer... params) {
        ... (As in above answer)
    }

    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        try {
            Intent intent = new Intent(Loading.this, Main.class);
            // Alternatively
            // Intent intent = new Intent(getApplication(), Main.class);
            startActivity(intent);
        } catch (ActivityNotFoundException e){
            // Exception handling code
        }
    } 
}
}

I think what your trying to say is that it's not switching activity? I hear what you say about scope, are you sure both classes are in the same file? ie, one class (LoadingTask) inside another (Loading class)?

try switching "Loading.this" for "getApplication()"

I have

public class Loading extends Activity {
protected void onCreate(...){
    ... (As in above answer)
}

// LoadTask exists inside the Loading activity class
private class LoadTask extends AsyncTask<Integer, Integer, Integer>     {
    protected Integer doInBackground(Integer... params) {
        ... (As in above answer)
    }

    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        try {
            Intent intent = new Intent(Loading.this, Main.class);
            // Alternatively
            // Intent intent = new Intent(getApplication(), Main.class);
            startActivity(intent);
        } catch (ActivityNotFoundException e){
            // Exception handling code
        }
    } 
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文