如何停止方法处理程序?

时间:2016-06-13 09:45:11

标签: android methods

如何停止这种方法,我需要在获得经度和经度时停止这种方法。

private final Runnable r = new Runnable() {
    public void run() {
        gps = new GPSTracker(MainMaps.this);
        if (gps.canGetLocation()) {

            latitude = gps.getLatitude();
            longitude = gps.getLongitude();

            Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
        }

        final String lat = String.valueOf(gps.getLatitude());
        final String longg = String.valueOf(gps.getLongitude());

        if (lat.toString() != "0.0" && longg.toString() != "0.0") {
            LatLng fromPosition = new LatLng(latitude, longitude);
            LatLng toPosition = new LatLng(-7.782939, 110.367050);

            md = new GoogleMapV2Direction();
            mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.fragment)).getMap();

            mMap.getUiSettings().setZoomControlsEnabled(true);
            mMap.getUiSettings().setCompassEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(true);

            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(fromPosition, 14));

            mMap.addMarker(new MarkerOptions().position(fromPosition)
                    .title("Posisi Anda"));
            mMap.addMarker(new MarkerOptions().position(toPosition).title("End"));
            getDirectionMap(fromPosition, toPosition);

            //handler.removeCallbacks(r);
            //onStop();
            //if (lat.toString() != "0.0" && longg.toString() != "0.0") {
             //   Toast.makeText(getApplicationContext(), "It works===" + lat + "=" + longg, Toast.LENGTH_SHORT).show();
                onStop();
           // }
        }

        Toast.makeText(getApplicationContext(), "It works" + lat + "=" + longg, Toast.LENGTH_SHORT).show();
        handler.postDelayed(this, 2000);
    }
};

protected void onStop() {
    super.onStop();
    handler.removeCallbacksAndMessages(r);
}

1 个答案:

答案 0 :(得分:0)

请试试这个 handler.removeCallbacksAndMessages(NULL);