在线程内部调用服务

时间:2014-11-07 12:13:23

标签: android multithreading maps

我正在开发使用GMaps API的Android应用。我在服务器上也有一个PSQL数据库,它可以计算地理信息,比如路线等。

用户点击“GO”按钮后,我想用我的服务器与用户地址的坐标联系,以获取我的数据库的路径路径。我正在“onResume()”内的Thread下运行这些proccesses,我不想执行AsyncTask,因为我希望用户等待直到从服务器收到路由。我怎样才能做到这一点?

的onResume()

//** Prevents map = null*/
@Override
public void onResume() {
    super.onResume();
    new Thread(){
        public void run(){
            while(mapFrag.getMap().equals(null)){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            runOnUiThread(new Runnable() {

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

configMap()

public void configMap(){

route=null;
resultAddress = new ArrayList<Address>();
routeList=new ArrayList<LatLng>();

//Go button
goButton=(Button) findViewById(R.id.goButton);

//From and target address
fromAddress= (TextView) findViewById(R.id.fromAddress);
toAddress= (TextView) findViewById(R.id.toAddress);

//Opens and defines the map
map=mapFrag.getMap();
map.setMapType(GoogleMap.MAP_TYPE_NORMAL);

LatLng mapPoint;

mapPoint=new LatLng(38.741284, -9.146643);

//Displays the map on the previous map coordinates
CameraPosition cameraPosition = new CameraPosition.Builder().target(mapPoint).zoom(14).build();
CameraUpdate update=CameraUpdateFactory.newCameraPosition(cameraPosition);

//Set the marker to current position
MarkerOptions currentOptions =new MarkerOptions();
currentOptions.draggable(false);
currentOptions.title("My Location");
currentOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
currentOptions.position(mapPoint);

//Displays the marker
currentPosition=map.addMarker(currentOptions);

的OnClick()

        //Gets the coordinates for the source and target of the route
        goButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                    Geocoder fromcoder = new Geocoder(Map.this);
                    LatLng fromLatlng = null;
                    List<Address> fromAddresses= new ArrayList<Address>();

                    Geocoder Tocoder = new Geocoder(Map.this);
                    LatLng toLatlng = null;
                    List<Address> toAddresses= new ArrayList<Address>();

                    //Saves coordinates of from address
                    try {
                        fromAddresses=getLocation(fromAddress.getText().toString() , fromAddresses);

                        //Saves coordinates of to address
                        toAddresses = getLocation((String) toAddress.getText().toString() , toAddresses);

                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                            //Removes previous From marker
                            if(markerFrom!=null) markerFrom.remove();
                            //Removes previous To marker
                            if(markerTo!=null) markerTo.remove();

                    /**
                    *   Here I want to CONNECT and Get the route to print on the map
                    *   (Preference to call another class, because I don't want to put all code
                    *    in here)
                    */


}
}

1 个答案:

答案 0 :(得分:0)

  

我不想执行AsyncTask,因为我希望用户等待直到从服务器收到路由

您可以使用AsynTask实现此目的。在progressBar中显示preExecute()并在doInBackground()

中执行任何服务器互动