getting the call logs of incoming and outgoing calls in android programmatically

前端 未结 5 1864
执笔经年
执笔经年 2021-02-02 04:34

I am making an app in which I want to get the call logs of all incoming, outgoing and missed calls. How can I do that?

5条回答
  •  旧时难觅i
    2021-02-02 05:12

    public class MainActivity extends Activity 
    {
    TextView textView = null;
    
    
    int callcode;
    String callType ;
    String phNum;
    Date callDate;
    String callTypeCode;
    String strcallDate;
    String callDuration;
    String currElement;
    static boolean ring = false;
    static boolean callReceived = false;
    
    
    StringBuffer sb = new StringBuffer();
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textview_call);
    
    
                    SmsManager sms = SmsManager.getDefault();
                    String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
    
                    /* Query the CallLog Content Provider */
                    @SuppressWarnings("deprecation")
                    Cursor managedCursor = managedQuery(CallLog.Calls.CONTENT_URI, null,
                            null, null, strOrder);
    
                    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 Log :");
    
    
    
                    if(managedCursor.moveToFirst())
                    {
                        String phNum = managedCursor.getString(number);
    
                        String callTypeCode = managedCursor.getString(type);
    
                        String strcallDate = managedCursor.getString(date);
    
                        Date callDate = new Date(Long.valueOf(strcallDate));
    
                        String callDuration = managedCursor.getString(duration);
    
                        String callType = null;
    
                        int callcode = Integer.parseInt(callTypeCode);
    
                        switch (callcode) 
                        {
                        case CallLog.Calls.OUTGOING_TYPE:
    
    
                            callType = "Outgoing";
    
    
    
                            //sms.sendTextMessage(phNum, null, "Outgoing msg",  null, null);  
    
    
                            break;
                        case CallLog.Calls.INCOMING_TYPE:
    
                            callType = "Incoming";
                            //sms.sendTextMessage(phNum, null, "Incoming msg",  null, null);  
    
    
                            break;
                        case CallLog.Calls.MISSED_TYPE:
    
                            callType = "Missed";
                        //sms.sendTextMessage(phNum, null, "Missed msg",  null, null);  
    
                            break;
                        }
                        sb.append("\nPhone Number:--- " + phNum + " \nCall Type:--- "
                                + callType + " \nCall Date:--- " + callDate
                            + " \nCall duration in sec :--- " + callDuration);
                        sb.append("\n----------------------------------");
                    }
    
    
                    managedCursor.close();
                textView.setText(sb);
    
    }
    
    }    
    

提交回复
热议问题