从服务中存在的timertask(在线程内运行)内运行线程时出错

时间:2013-05-31 05:43:32

标签: android multithreading service timer

我有Service运行thread。此thread运行timertask,此timertask现在必须运行另一个thread

我的Service班级看起来像这样:

    Timer pTimer;


    int Time = 1;
    int samplerate=100;
    int sampleSize=((Time*1000)/samplerate);

    Accelerometer_Thread Ac_Thread;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
pTimer = new Timer();
         Ac_Thread = new Accelerometer_Thread();  

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        // TODO Auto-generated method stub
        //super.onStart(intent, startId);


        Ac_Thread.start();

        return START_STICKY;
    }

    public class Accelerometer_Thread extends Thread{
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            pTimer.scheduleAtFixedRate(task,0, samplerate);
        }
    }

    /* The Timertask which is to be executed at a periodic interval of "samplerate" */
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
                // TODO Auto-generated method stub


                            String Ack = null;
                            try {
                                Ack = Post.PostToWebService("Hello");
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                            }

                            if (Ack.contentEquals("1")) {
                                //Do SOmething
                            }
                            else
                                Log.d("Error", "NotSent To Webservice");


                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                    }

                }
        }
    };

Post.PostToWebService ehich启动Thread看起来像这样:

 public static String ReceivedAcknowledgement=null;

        public static String buffer;
    public static String PostToWebService(String Payload){

            CallPost cp = new CallPost();
            buffer = Payload;

            ReceivedAcknowledgement="STRT";
            try {
                cp.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cp.start();

            while(ReceivedAcknowledgement=="STRT")
            {
                try
                {
                    Thread.sleep(10);

                }catch(Exception ex)
                {

                }
            }
            if(ReceivedAcknowledgement != "STRT")
            return ReceivedAcknowledgement;
            else
                return null;
}

CallPost看起来像这样:

public class CallPost extends Thread{


    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();

        Post.ReceivedAcknowledgement = ParseXML(in);

    }

    private String ParseXML(InputStream i){

        //Parse

    }
}

我在行Post.PostToWebService("Hello")的服务类中收到错误 错误说明的地方:

  

java.lang.RuntimeException:无法在线程内创建处理程序   没有调用Looper.prepare()

我已经尝试过了解loopers和handlers但我有点无法得到它。

需要尽快提供帮助。

0 个答案:

没有答案