【问题标题】:Calling a service inside thread在线程内调用服务
【发布时间】:2014-11-07 12:13:23
【问题描述】:

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

用户点击“GO”按钮后,我想用用户地址的坐标联系我的服务器,从我的数据库中获取路由路径。我在“onResume()”内部的线程下运行这些进程,并且我不想执行 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)
                    */


}
}

【问题讨论】:

    标签: android multithreading maps


    【解决方案1】:

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

    您可以使用 AsynTask 实现此目的。在preExecute() 中显示progressBar 并在doInBackground() 中进行任何服务器交互

    【讨论】:

    • 嗯.. 但是如果我在调用那个 asyncTask 之后有一些代码,它会在任务之后执行吗?
    • 嗯...让我试试 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多