在handler和timerTask之间进行同步

时间:2015-05-20 04:47:57

标签: java android multithreading

我无法解决错误。无法在线程Looper.prepare()内创建处理程序。我为什么得到它?

    trip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            //timer = 30;


            try {
                timer = Integer.parseInt(time.getText().toString());
            } catch (NumberFormatException e) {
              //  Toast.makeText(getApplicationContext(), "Invalid Time!", Toast.LENGTH_LONG).show();
                startActivity(new Intent(MainActivity.this, MainActivity.class));
            }
            Log.d("tag", "Timer set");
            if (!flag1) {
                trip.setText("End Trip");
                Log.d("tag", "End Trip has been set");
                flag1 = true;
                timer1.schedule(new myTimerTask(), 0, timer * 60 * 1000);
            } else {
                timer1.cancel();
              //  Toast.makeText(getApplicationContext(), "Trip Ended..Thank You!!", Toast.LENGTH_LONG).show();
                finish();
            }

        }

    });
}


public class myTimerTask extends TimerTask {
    LocationListener locationListener;
    Handler handler;
    @Override
    public void run() {




        Location gpsLocation = null;

        final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        final boolean isActive = true;
        final long time = timer * 60 * 1000;
        final float distance = 1;


/*    if (locationManager != null) {
        gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    }
else{
    gpsLocation=null;
}*/

        handler = new Handler() {

            @Override
            public void handleMessage(Message msg) {

                if (isActive)

                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, distance, locationListener);

                super.handleMessage(msg);
            }
        };
        locationListener = new myLocationListener(handler);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, time, distance, locationListener);
        LocationAddress locationAddress=new LocationAddress();
        locationAddress.getAddressFromLocation(getApplicationContext(),number.getText().toString(),timer,gpsLocation);
    }


}
class myLocationListener implements LocationListener{
    Handler handler;
    public myLocationListener(Handler handler)
    {
        this.handler=handler;
    }
    @Override
    public void onLocationChanged(Location location) {
        Thread thread=new Thread(new Runnable()
        {

            @Override
            public void run()
            {
                Message msg=Message.obtain();
                handler.sendMessageDelayed(msg,1000);

            }
        });`
        thread.run();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

错误是:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
            at android.os.Handler.<init>(Handler.java:197)
            at android.os.Handler.<init>(Handler.java:111)
            at com.mycompany.s.MainActivity$myTimerTask$1.<init>(MainActivity.java:110)
            at com.mycompany.s.MainActivity$myTimerTask.run(MainActivity.java:110)
            at java.util.Timer$TimerImpl.run(Timer.java:284)

0 个答案:

没有答案