【问题标题】:is this asynctask correct?这个异步任务正确吗?
【发布时间】:2012-01-06 00:37:05
【问题描述】:

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

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

 }

我希望它启动异步任务,它将 5 个声音加载到音板中,完成后,将 int "switchscreensvalue" 更改为 1。然后,加载屏幕应该在 "switchscreensvalue" 时更改为主屏幕= 1. 但它不起作用。请任何人帮助我,只是第一次学习异步任务。事实上,Java 还是相当新的。我需要 asynctask 加载 5 个声音,然后将活动从加载更改为主要活动。

【问题讨论】:

    标签: android android-activity android-asynctask


    【解决方案1】:

    那是因为你在打电话

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

    在您无法保证的 onCreate() 中将再次调用。

    你为什么不打电话

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

    在你设置变量后的 onPostExecute() 中。

    【讨论】:

    • 记住 asynctask 不是在 man 线程上运行的,所以在 onCreate 执行 asynctask 之后它将继续运行你的代码,所以当 asynctask 完成时 onCreate 已经完成了它的检查变量。
    • 你是什么意思改变加载到主?(在下面的评论答案中)
    • 我的意思是我需要将活动从 Loading.class 更改为 Main.class。但是如果我在 asynctask.class 中,我不知道该怎么做
    【解决方案2】:

    这应该可以...

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

    【讨论】:

    • 这很好,先生。你刚刚教了我一些关于类和异步任务的基本概念。也非常感谢 0909EM 和 coder_for_life。我要将此页面添加为书签,并永远记住你们!
    • 我第一次尝试使用AsyncTask 有点失败。我在这里学到了 SO 的技巧,我认为传递它们是公平的。使用AsyncTask 的最简单方法是如上所示 - 使其成为Activity 的嵌套/内部类。这样它就可以使用onPreExecuteonProgressUpdateonPostExecute 等访问您的所有Activity 字段和方法。可以创建独立的AsyncTask 子类,但这会更复杂。祝你好运。
    • 实际上我刚刚编辑了我的代码以将startActivity 更改为startMainActivity,因此它不会与现有的startActivity 混淆 - 我建议你也这样做。
    【解决方案3】:

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

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

    【讨论】:

    • 呃....被打败了...抱歉@coder_For_Life22 似乎我们都得到了相同的答案...
    • 是的,但问题是,在代码中: Intent myIntent = new Intent(Loading.this, main.class);这不在“加载”类中,它在单独的类中。如何解决此问题,以便将 Loading 更改为 Main?
    • 如果我没记错的话,“Loading.this”是您从 AsyncTask 引用“Loading”类的方式(当 AsyncTask 与 Loading Activity 驻留在同一个 Java 文件中时)...或者, Intent myIntent = new Intent(getApplication(), Main.class);它将应用程序的上下文用于“主要”活动......否则我误解了你的评论......
    • @0909EM:不需要在onPostExecute(...) 中调用isCancelled()。原因是isCancelled() 只会在cancel(true)AsyncTask 调用时才返回true。如果确实如此,则永远不会调用onPostExecute(...),而是调用onCancelled()
    • 我的加载屏幕活动在一个类中,我查看了 android 开发者网站,它说要创建一个新类来制作异步任务。现在,当我尝试使用 Intent myIntent = new Intent(Loading.this, main.class);在我的 asynctask 类中,它说它不在可访问范围内。这就是我现在对 onPostExecute protected void onPostExecute(Integer result){ if (!isCancelled() && (result != null) && (result > 0)) { Intent myIntent = new Intent(Loading.this, dirtynames 。班级); startActivityForResult(myIntent, 0); } }
    【解决方案4】:

    我认为您想说的是它不是切换活动?我听到您对范围的看法,您确定两个类都在同一个文件中吗?即,一个类(LoadingTask)在另一个(Loading 类)中?

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

    我有

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

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 2016-07-16
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 2016-05-12
      • 2012-06-03
      • 1970-01-01
      相关资源
      最近更新 更多