在循环内启动AsyncTask

时间:2014-04-09 10:40:32

标签: java android android-asynctask activemq

我想连续向我的服务器发送数据,所以我创建了两个AsyncTask,一个用于连接服务(工作正常),另一个用于发送数据。

问题是,第二个产生了这个错误:

04-09 12:29:00.039: E/AndroidRuntime(8397): FATAL EXCEPTION: serviceThread
04-09 12:29:00.039: E/AndroidRuntime(8397): Process: com.example.stomp2, PID: 8397
04-09 12:29:00.039: E/AndroidRuntime(8397): java.lang.IllegalStateException: Cannot execute task: the task is already running.
04-09 12:29:00.039: E/AndroidRuntime(8397):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:576)
04-09 12:29:00.039: E/AndroidRuntime(8397):     at android.os.AsyncTask.execute(AsyncTask.java:535)
04-09 12:29:00.039: E/AndroidRuntime(8397):     at com.example.stomp2.ServiceConexion$1$1.run(ServiceConexion.java:189)
04-09 12:29:00.039: E/AndroidRuntime(8397):     at java.lang.Thread.run(Thread.java:841)

这是我的代码:

public class ServiceConexion extends Activity {

    private String opcion, ip;
    private int i = 0, puerto;
    private boolean server;
    public IRemoteService mRemoteService;
    private Thread serviceThread = null;
    private float message5, message6;
    private float lastx, lasty;
    private Client c;
    private Connect connect;
    private Send send;

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);



        // CONECT WITH SERVER//
        connect = new Connect();
        connect.execute();
        send = new Send();

        // CONECT WITH REMOTE SERVICE
        serviceIntent.setClassName("org.gradiant.sistole.locservice",
                "org.gradiant.sistole.locservice.DemoService");

        boolean ok = bindService(serviceIntent, mServiceConnection,
                Context.BIND_AUTO_CREATE);
        Log.v("ok", String.valueOf(ok));

    }


    @Override
    protected void onPause() {
        i = 1;
        unbindService(mServiceConnection);
        finish();
        super.onPause();
    }


    private ServiceConnection mServiceConnection = new ServiceConnection() {


        @Override
        public void onServiceDisconnected(ComponentName name) {

        }


        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {

            mRemoteService = IRemoteService.Stub.asInterface(service);
            i = 0;

            serviceThread = new Thread(new Runnable() {
                @Override
                /**Bucle mediante el cual se recogen las distancias a los transmisores, las coordenadas, y donde se llama a la clase que actualizá la interfaz*/
                public void run() {
                    while (i == 0) {

                        try {

                            send.execute(5.0);

                        }

                        catch (RemoteException e) {

                            Intent serviceIntent = new Intent();
                            serviceIntent.setClassName(
                                    "org.gradiant.sistole.locservice",
                                    "org.gradiant.sistole.locservice.DemoService");
                            boolean ok = bindService(serviceIntent,
                                    mServiceConnection,
                                    Context.BIND_AUTO_CREATE);

                        }

                    }
                }

            }, "serviceThread");
            serviceThread.start();

        }

    };

    public class Connect extends AsyncTask<Void, Void, Void> {

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

                c = new Client("10.5.1.72", 61613, "", "");


            }

            catch (LoginException e) {

                e.printStackTrace();
            }

            catch (IOException e) {

                e.printStackTrace();
            }
            return null;
        }

    }

    public class Send extends AsyncTask<Float, Void, Void> {

        @Override
        protected Void doInBackground(Float... params) {
            c.begin();
            c.send("prueba", String.valueOf(params));
            c.commit();
            return null;
        }

    }

}

2 个答案:

答案 0 :(得分:0)

你可以调用两个AsyncTask,因为它们有点Thread .. 当一个AsyncTask在后​​台执行时,您正尝试执行AsyncTask生成的第二个Error,...尝试在send.execute()方法中调用OnPostExecute() Connect课程..

答案 1 :(得分:0)

你在while循环中有这一行:

send.execute(5.0);

AsyncTask的实例只能运行一次。将其更改为:

new Send().execute(5.0f);

尽管如此,我不确定其余部分会按预期运行。