传递参数的AsyncTask [英] Passing parameters to Asynctask

查看:107
本文介绍了传递参数的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的异步任务,您可以通过菜单中的活动串并加载了一些stuff..but我是 不能做so..Am使用它以正确的方式我和我是正确传递参数? 请参阅code段。谢谢

 私有类设置扩展的AsyncTask<太虚,整型,太虚> {

    @覆盖
    保护无效doInBackground(虚空...... PARAMS){
        尝试 {
            如果(!(getIntent()。getExtras()。的isEmpty())){
                捆绑gotid = getIntent()getExtras()。
                标识符= gotid.getString(钥匙);
            }
        }赶上(例外五){
            e.getStackTrace();
        } 最后 {

            如果(identifier.matches(ABC)){
                publishProgress(0);
                db.insert_fri();
            }否则,如果((identifier.matches(XYZ))){
                publishProgress(1);
                db.insert_met();
            }
        }
        返回null;
    }

    @覆盖
    保护无效onProgressUpdate(整数...我){
        //这里开始播放歌曲
        如果(I [0] == 0){
            song.setLooping(真正的);
            song.start();
        }
    }

    @覆盖
    保护无效onPostExecute(无效RES){

    }

    @覆盖
    在preExecute保护无效(){
        //做执行前的东西
    }
}
 

解决方案

取而代之的是我会做

 私有类设置扩展的AsyncTask<字符串,整数,太虚> {

    @覆盖
    保护无效doInBackground(字符串... PARAMS){
    字符串标识符= PARAMS [0];

          如果(identifier.matches(ABC)){
                publishProgress(0);
                db.insert_fri();
            }否则,如果((identifier.matches(XYZ))){
                publishProgress(1);
                db.insert_met();
            }
        }
        返回null;
    }

    @覆盖
    保护无效onProgressUpdate(整数...我){
        //这里开始播放歌曲
        如果(I [0] == 0){
            song.setLooping(真正的);
            song.start();
        }
    }

    @覆盖
    保护无效onPostExecute(无效RES){

    }

    @覆盖
    在preExecute保护无效(){
        //做执行前的东西
    }
}
 

和调用AsyncTask的创建一个AsyncTask的的prevent开销之前检查标识

像这样

 如果(!(getIntent()。getExtras()的isEmpty())){
                捆绑gotid = getIntent()getExtras()。
                标识符= gotid.getString(钥匙);
               新设置()执行(标识);
    }
 

I am using Async tasks to get string from the menu activity and load up some stuff..but i am not able to do so..Am i using it in the right way and am i passing the parameters correctly? Please see the code snippet. thanks

  private class Setup extends AsyncTask<Void, Integer, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            if (!(getIntent().getExtras().isEmpty())) {
                Bundle gotid = getIntent().getExtras();
                identifier = gotid.getString("key");
            }
        } catch (Exception e) {
            e.getStackTrace();
        } finally {

            if (identifier.matches("abc")) {
                publishProgress(0);
                db.insert_fri();
            } else if ((identifier.matches("xyz"))) {
                publishProgress(1);
                db.insert_met();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... i) {
        // start the song here
        if (i[0] == 0) {
            song.setLooping(true);
            song.start();
        }
    }

    @Override
    protected void onPostExecute(Void res) {

    }

    @Override
    protected void onPreExecute() {
        // do something before execution
    }
}

解决方案

Instead of this i would do

 private class Setup extends AsyncTask<String, Integer, Void> {

    @Override
    protected Void doInBackground(String... params) {
    String identifier = params[0];

          if (identifier.matches("abc")) {
                publishProgress(0);
                db.insert_fri();
            } else if ((identifier.matches("xyz"))) {
                publishProgress(1);
                db.insert_met();
            }
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... i) {
        // start the song here
        if (i[0] == 0) {
            song.setLooping(true);
            song.start();
        }
    }

    @Override
    protected void onPostExecute(Void res) {

    }

    @Override
    protected void onPreExecute() {
        // do something before execution
    }
}

and check for "identifier" before invoking the asynctask to prevent overhead of creating a AsyncTask

like this

if (!(getIntent().getExtras().isEmpty())) {
                Bundle gotid = getIntent().getExtras();
                identifier = gotid.getString("key");
               new Setup().execute(identifier);
    }

这篇关于传递参数的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆