开发者

Error when using URL conenction with thread in MapActivity in android

I am using a URL connection to connect Google maps web service and get the result in KML format and parse it and display it on MapView. I put this operations in a thread, but I get this error that I don't know what does it mean ?! please help me in finding out what is this error and what is the solution ?

09-28 23:00:45.026: ERROR/ActivityManager(67): ANR in com.android.uis (com.android.uis/.RoutingMapActivity)
09-28 23:00:45.026: ERROR/ActivityManager(67): Reason: keyDispatchingTimedOut
09-28 23:00:45.026: ERROR/ActivityManager(67): Load: 0.49 / 0.21 / 0.15
09-28 23:00:45.026: ERROR/ActivityManager(67): CPU usage from 22714ms to 0ms ago:
09-28 23:00:45.026: ERROR/ActivityManager(67):   99% 1049/com.android.uis: 98% user + 0.4% kernel / faults: 159 minor
09-28 23:00:45.026: ERROR/ActivityManager(67):   0.4% 199/com.google.process.gapps: 0.2% user + 0.1% kernel / faults: 52 minor
09-28 23:00:45.026: ERROR/ActivityManager(67):   0.1% 67/system_server: 0% user + 0% kernel
09-28 23:00:45.026: ERROR/ActivityManager(67):   0% 40/adbd: 0% user + 0% kernel
09-28 23:00:45.026: ERROR/ActivityManager(67):   0% 131/com.android.systemui: 0% user + 0% kernel / faults: 1 minor
09-28 23:00:45.026: ERROR/ActivityManager(67): 100% TOTAL: 99% user + 0.7% kernel
09-28 23:00:45.026: ERROR/ActivityManager(67): CPU usage from 996ms to 1566ms later:
09-28 23:00:45.026: ERROR/ActivityManager(67):   91% 1049/com.android.uis: 91% user + 0% kernel / faults: 2 minor
09-28 23:00:45.026: ERROR/ActivityManager(67):     89% 1049/com.android.uis: 89% user + 0% kernel
09-28 23:00:45.026: ERROR/ActivityManager(67):   10% 67/system_server: 5.2% user + 5.2% kernel / faults: 6 minor
09-28 23:00:45.026: ERROR/ActivityManager(67):     8.7% 97/InputDispatcher: 3.5% user + 5.2% kernel
09-28 23:00:45.026: ERROR/ActivityManager(67):     1.7% 68/HeapWorker: 0% user + 1.7% kernel
09-28 23:00:45.026: ERROR/ActivityManager(67): 100% TOTAL: 94% user + 5.2% kernel

MapActivity class

package com.android.uis;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.android.geo.Road;
import com.android.geo.RoadProvider;
import com.android.maps.MyItemizedOverlay;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class RoutingMapActivity extends MapActivity implements LocationListener {

    // Key : 0ObBrVVNjsIA__m7D1lmsednIXg6pcDRBG-Qvfg
    MapView mapView;
    List<Overlay> mapOverlays;
    Drawable drawable;
    Drawable drawable2;
    MyItemizedOverlay itemizedOverlay;
    MyItemizedOverlay itemizedOverlay2;
    LocationManager locationManager;
    private String provider;
    private Road mRoad;
    private Handler mHandler;
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_layout);
        initMap();
        //initMyLocation();

    }
    private void initMap(){
        mapView = (MapView) findViewById(R.id.google_maps);
        mapView.setBuiltInZoomControls(true);       
        mapOverlays = mapView.getOverlays();

        new Thread(){


            @Override
            public void run(){
                double fromLat = 49.85, fromLon= 24.016667, toLat = 50.45, toLon = 30.523333;
                String url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
                InputStream is = getConnection(url);
                mRoad = RoadProvider.getRoute(is);
                mHandler.sendEmptyMessage(0);

            }
        }.start();

        mHandler = new Handler(){
            public void handleMessage(Message msg){
                //drawable2 = getResources().getDrawable(R.drawable.marker2);
                drawable = getResources().getDrawable(R.drawable.marker);
                itemizedOverlay2 = new MyItemizedOverlay(drawable, mapView,mRoad);
                mapOverlays.clear();
                for(int i=0; i<mRoad.mRoute.length;i++){
                    addPoint("","",new GeoPoint((int)(mRoad.mRoute[i][1]*1E6),(int)(mRoad.mRoute[i][0]*1E6)),itemizedOverlay2);
                }
                //addPoint("Point 2.","Point 2.",(int)(51.515259*1E6),(int)(-0.086623*1E6),itemizedOverlay2);
                mapOverlays.add(itemizedOverlay2);
                mapView.invalidate();
            };
        };


    }
    private InputStream getConnection(String url){
        InputStream is = null;
        try {
            URLConnection urlConn= new URL(url).openConnection();
            is= urlConn.getInputStream();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return is;
    }
    private void addPoint(String name,String description,GeoPoint point, MyItemizedOverlay myItemizedOverlay){
        OverlayItem overlayItem = new OverlayItem(point,name,description);
        myItemizedOverlay开发者_开发技巧.addOverlay(overlayItem);
    }
    private void initMyLocation(){
        locationManager =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria=new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

    }
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    @Override
    public void onLocationChanged(Location arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}

Edit

I have changed the code to the following .. but still have the same error

private void initMap(){ 
        new DrawRoad().execute("");
    }
    private class DrawRoad extends AsyncTask<String, Void, Road> {
         protected Road doInBackground(String... unused) {
            mapOverlays = mapView.getOverlays();
            double fromLat = 49.85, fromLon= 24.016667, toLat = 50.45, toLon = 30.523333;
            String url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon);
            InputStream is = getConnection(url);
            mRoad = RoadProvider.getRoute(is);
            return mRoad;
         }

         protected void onPostExecute(Road road) {
            drawable = getResources().getDrawable(R.drawable.marker);
            itemizedOverlay2 = new MyItemizedOverlay(drawable, mapView,road);
            mapOverlays.clear();
            for(int i=0; i<mRoad.mRoute.length;i++){
                addPoint("","",new GeoPoint((int)(road.mRoute[i][1]*1E6),(int)(road.mRoute[i][0]*1E6)),itemizedOverlay2);
            }
            //addPoint("Point 2.","Point 2.",(int)(51.515259*1E6),(int)(-0.086623*1E6),itemizedOverlay2);
            mapOverlays.add(itemizedOverlay2);
            mapView.invalidate();
         }
     }


You could try using an async task which is simpler, something like:

    private class GetKMLTask extends AsyncTask<String, Integer, String> {
    protected String doInBackground(String... unused) {
        double fromLat = 49.85, fromLon= 24.016667, toLat = 50.45, toLon = 30.523333;
        String url = "";
        InputStream is = getConnection(url);
        //.... get the road here    
        return result;
    }

    protected void onPostExecute(String jsonResult) {
        try {
            drawable = getResources().getDrawable(R.drawable.marker);
            itemizedOverlay2 = new MyItemizedOverlay(drawable, mapView,mRoad);
            mapOverlays.clear();
            for(int i=0; i<mRoad.mRoute.length;i++){
                addPoint("","",new GeoPoint((int)(mRoad.mRoute[i][1]*1E6),(int)(mRoad.mRoute[i][0]*1E6)),itemizedOverlay2);
            }
            //addPoint("Point 2.","Point 2.",(int)(51.515259*1E6),(int)(-0.086623*1E6),itemizedOverlay2);
            mapOverlays.add(itemizedOverlay2);
            mapView.invalidate();
            } 
        catch (Exception e) {
            onFail("");
        }

    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜