how to get call time duration in android ? and also multiple time write data in file?
<pre><code> public void onCallStateChanged(int state,String incomingNumber)
{
    System.out.print("\nState :-  "+state);
      switch(state){
        case TelephonyManager.CALL_STATE_IDLE:

            if(flag==true &amp;&amp; ringflag == true)
            {
                flag=false;
                ringflag=false;
                System.out.print("\nflag = " + flag);
                System.out.print("\nringflag = " + ringflag);
                stop = System.currentTimeMillis();
                System.out.println("\nTotal time : " +stop);
                System.out.println("\nTotal time : " +(stop - start)/1000);
                System.out.println("\nIDLE : " + incomingNumber);
                long time = (stop - start) / 1000;
                String path = Environment.getExternalStorageDirectory().getAbsolutePath();
                f = new File(path + "/sms.txt");
                if (f.exists()) {
                    try {
                        raf =new RandomAccessFile(f, "rw");
                        long pointer = raf.length();
                        raf.seek(pointer);
                        String data = ":-"+no+","+time;
                        raf.writeBytes(data);
                        raf.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                } else {
                    try {
                        raf = new RandomAccessFile(f,"rw");
                        String data = ":-"+no+","+time;
                        raf.writeBytes(data);
                        raf.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            if(ringflag == true)
            {
                System.out.println("OFFHOOK :- " + incomingNumber);
                start = System.currentTimeMillis();
                System.out.print("\nStart is :-" + start);
                flag=true;
            }
        break;
        case TelephonyManager.CALL_STATE_RINGING:
            no = incomingNumber;
            System.out.println("Ringing : " + incomingNumber);
            ringflag= true;
        break;
      }
  }
</code></pre>
