Android Developers Blog

Monday 25 June 2012

Lecture On Vritual Memory Management


Google I/o Memory management


How to draw path using Google Directions Api on Native maps and also Display instructions

     DrawPath(points.get(i), points.get(i+1), Color.BLUE, map , i);
ArrayList<String> distance =new ArrayList<String>();


private void DrawPath(GeoPoint src,GeoPoint dest, int color, MapView mMapView01 , int k)
    {
    // connect to map web service
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.google.com/maps/api/directions/json?origin=");
    //urlString.append("&saddr=");//from
    urlString.append( Double.toString((double)src.getLatitudeE6()/1.0E6 ));
    urlString.append(",");
    urlString.append( Double.toString((double)src.getLongitudeE6()/1.0E6 ));
    urlString.append("&destination=");//to
    urlString.append( Double.toString((double)dest.getLatitudeE6()/1.0E6 ));
    urlString.append(",");
    urlString.append( Double.toString((double)dest.getLongitudeE6()/1.0E6 ));
    urlString.append("&sensor=false");
//    Log.d("xxx","URL="+urlString.toString());
    JSONObject jObject = null;
   
    String data = null;
    try {
        data = JSONfunctions.getJSONfromURL(urlString.toString());
        jObject = new JSONObject(data);
    } catch (UnknownHostException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    catch (SocketTimeoutException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
//    Log.d("Getting Json Object",""+jObject);
   
    try {
        if(!jObject.get("status").equals("ZERO_RESULTS"))
        {
            JSONArray JsonSteps = null;
           
            String startAddress;
            String endAddress;
            String distance;
            try {
                JsonSteps = jObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONArray("steps");
                startAddress = jObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).get("start_address").toString();
                endAddress = jObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).get("end_address").toString();
                distance= jObject.getJSONArray("routes").getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").get("text").toString();
                directions.add(" "+distance);
                directions.add("("+startAddress+" "+getResources().getString(R.string.to)+" "+endAddress+")"+getResources().getString(R.string.newLine));
                } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d("Json length", ""+JsonSteps.length());
           
            for(int i=0;i<JsonSteps.length();i++)
            {
                try {
                    Double slat = Double.parseDouble(JsonSteps.getJSONObject(i).getJSONObject("start_location").get("lat").toString());
                    String str=JsonSteps.getJSONObject(i).get("html_instructions").toString();
                   
           
                    str=str.replaceAll("<b>","");
                    str=str.replaceAll("</b>","");
                    str=str.replaceAll("</div>","");
                    str=str.replaceAll("<div style=","");
                    str=str.replaceAll("font-size:0.9em","");
                    str=str.replaceAll(">","");
                    str=str.replaceAll("<b>","");
                  directions.add((i+1)+" "+str);
               
                    Double slng = Double.parseDouble(JsonSteps.getJSONObject(i).getJSONObject("start_location").get("lng").toString());
                    Double dlat = Double.parseDouble(JsonSteps.getJSONObject(i).getJSONObject("end_location").get("lat").toString());
                    Double dlng = Double.parseDouble(JsonSteps.getJSONObject(i).getJSONObject("end_location").get("lng").toString());
                    MyOverLay my=new MyOverLay(new GeoPoint((int)(slat*1E6),(int)(slng*1E6)),new GeoPoint((int)(dlat*1E6),(int)(dlng*1E6)),2,color);
                    mMapView01.getOverlays().add(my);
                } catch (NumberFormatException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }


    }

How to delete a contact in android?



This simple code does that.

 public static boolean deleteContact(Context ctx, String phone, String name) {
        Uri contactUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
        Cursor cur = ctx.getContentResolver().query(contactUri, null, null, null, null);
        try {
            if (cur.moveToFirst()) {
                do {
                    if (cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME)).equalsIgnoreCase(name)) {
                        String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                        Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
                        ctx.getContentResolver().delete(uri, null, null);
                        return true;
                    }

                } while (cur.moveToNext());
            }

        } catch (Exception e) {
            System.out.println(e.getStackTrace());
        }
        return false;
    }