开发者

how to get call time duration in android ? and also multiple time write data in file?

 public void onCallStateChanged(int state,String incomingNumber)
{
    System.out.print("\nState :-  "+state);
      switch(state){
        case TelephonyManager.CALL_STATE_IDLE:

            if(flag==true && 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) {开发者_如何学C
                        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;
      }
  }


I can answer the first part of your question

To get the call duration it is important to access the Call Logs.

Using the information given in CallLog.Calls, It can be done like below:

          Uri allCalls = Uri.parse("content://call_log/calls");
          Cursor c = managedQuery(allCalls, null, null, null, null);
            for(String colName : c.getColumnNames())
                Log.v(TAG, "Column Name: " + colName);

            if (c.moveToFirst())
            {
               do{
                   String id = c.getString(c.getColumnIndex(CallLog.Calls._ID));
                   String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));
                   int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));

                   String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));
                   System.out.println("call time duration is"+duration);

                    switch (type)
                    {
                        case 1: Log.v(TAG, id + ", " +num + ": INCOMING") ; break;
                        case 2: Log.v(TAG, id + ", " +num + ": OUTGOING") ;break;
                        case 3: Log.v(TAG, id + ", " +num + ": MISSED") ; break;
                    }
               } while (c.moveToNext());
            }

Refer this nice blog post for more information.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜