0

我有一个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开始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,但我无法理解。

4

0 回答 0