auto backup of android calllog like when certain event like ringing of the phone it should log it in to a file but not working
<p>I have created one app for creating backup file of android call log. I received call log data and stored it in a <code>xml</code>. <br>When I receive some phone call it should log it into that file means update a log file for that I have used <code>phonestatelistener</code> with broadcast receiver but when I call it app crashes.<br>I am using a service which starts a service in background.and when call rings I called call log method that is described in my code and I used <code>callstatelistener</code> in broadcast receiver.</p>

<pre><code>public void getCallDetails() {

   StringBuffer sb = new StringBuffer();
   Cursor managedCursor = managedQuery( CallLog.Calls.CONTENT_URI,null, null,null, null);
   int number = managedCursor.getColumnIndex( CallLog.Calls.NUMBER ); 
   int type = managedCursor.getColumnIndex( CallLog.Calls.TYPE );
   int date = managedCursor.getColumnIndex( CallLog.Calls.DATE);
   int duration = managedCursor.getColumnIndex( CallLog.Calls.DURATION);
   sb.append( "Call Details :");


    try {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        // root elements
         doc = docBuilder.newDocument();
        rootElement = doc.createElement("Calllogs");
        doc.appendChild(rootElement);

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   while ( managedCursor.moveToNext() ) {
   String phNumber = managedCursor.getString( number );
   String callType = managedCursor.getString( type );
   String callDate = managedCursor.getString( date );
   Date callDayTime = new Date(Long.valueOf(callDate));
   String callDuration = managedCursor.getString( duration );
   String dir = null;
   int dircode = Integer.parseInt( callType );
   switch( dircode ) {
   case CallLog.Calls.OUTGOING_TYPE:
   dir = "OUTGOING";
   break;

   case CallLog.Calls.INCOMING_TYPE:
   dir = "INCOMING";
   break;

   case CallLog.Calls.MISSED_TYPE:
   dir = "MISSED";
   break;
   }
   sb.append( "\nPhone Number:--- "+phNumber +" \nCall Type:--- "+dir+" \nCall Date:--- "+callDayTime+" \nCall duration in sec :--- "+callDuration );
   sb.append("\n----------------------------------");
  dbHelper = new DatabaseHelper(this);
   dbHelper.InsertCallLog(phNumber, dir, callDayTime.toString(), "0", "0", callDuration);

  Element ephNumber = doc.createElement("PhoneNumber");
   ephNumber.appendChild(doc.createTextNode(phNumber));
   rootElement.appendChild(ephNumber);

   Element eCallType = doc.createElement("CallType");
   eCallType.appendChild(doc.createTextNode(dir));
   rootElement.appendChild(eCallType);

   Element eCallDate = doc.createElement("CallDate");
   eCallDate.appendChild(doc.createTextNode(callDayTime.toString()));
   rootElement.appendChild(eCallDate);

   Element eCallDuartion = doc.createElement("CallDuration");
   eCallDuartion.appendChild(doc.createTextNode(callDuration));
   rootElement.appendChild(eCallDuartion);

// write the content into xml file

        try {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);

StreamResult result = new StreamResult(new File(Environment.getExternalStorageDirectory()+"/CallLog.xml"));
    transformer.transform(source, result);


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


        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);



        System.out.println("File saved!");
   }

  managedCursor.close();
   //call.setText(sb);
  txtInfo.setText(sb);

   }
</code></pre>
