How to read call logs ?

Hi,

How to read call logs like Missed, Dialed, Received calls using Mono for Android?

Answers

  • CodeMonkeyCodeMonkey GBMember ✭✭✭

    Google found this: http://stackoverflow.com/questions/5910906/i-want-read-call-logs-in-the-android

    Try port it over, doesn't look to hard.

  • BulletBullet INMember

    I tried that link but am not able to make it in mono for android. Can someone ?

  • JonathanPryorJonathanPryor USXamarin Team Xamurai

    @Bullet: Perhaps this code would work for you?

    I've verified that this is the case, at least on Android 4.2 Jelly Bean; my device is unrooted, my app has the READ_LOGS permission, and I'm able to execute logcat. (Source is mostly the default template, with a added after the in Main.axml with an id of 'logcat'.)

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    
    using Android.App;
    using Android.Content;
    using Android.Runtime;
    using Android.Views;
    using Android.Widget;
    using Android.OS;
    
    namespace Scratch.Logcat
    {
        [Activity (Label = "Scratch.Logcat", MainLauncher = true)]
        public class Activity1 : Activity
        {
            int count = 1;
    
            protected override void OnCreate (Bundle bundle)
            {
                base.OnCreate (bundle);
    
                Console.WriteLine ("Scratch.Logcat");
    
                // Set our view from the "main" layout resource
                SetContentView (Resource.Layout.Main);
    
                int logcatLines = 0;
                using (var logcat = FindViewById<TextView> (Resource.Id.logcat))
                    Exec (new[]{"/system/bin/logcat", "-d"},
                            (o, e) => {
                                if (logcatLines++ > 10)
                                    return;
                                Console.WriteLine (e.Data);
                                logcat.Append ((e.Data ?? "") + "\n");
                                if (logcatLines == 10) {
                                    var p = (System.Diagnostics.Process) o;
                                    p.CancelErrorRead ();
                                    p.CancelOutputRead ();
                                    if (!p.HasExited) {
                                        try {
                                            p.Kill ();
                                        } catch (Exception ex) {
                                            Console.WriteLine ("Unable to kill: {0}", ex.Message);
                                        }
                                    }
                                }
                            },
                            (o, e) => {
                                Console.Error.WriteLine (e.Data);
                            }
                    );
    
    
                // Get our button from the layout resource,
                // and attach an event to it
                Button button = FindViewById<Button> (Resource.Id.myButton);
    
                button.Click += delegate {
                    button.Text = string.Format ("{0} clicks!", count++);
                };
            }
    
            static int Exec (string[] command, EventHandler<DataReceivedEventArgs> stdout, EventHandler<DataReceivedEventArgs> stderr)
            {
                var psi = new ProcessStartInfo (command [0], '"' + string.Join ("\" \"", command.Skip (1)) + '"') {
                    RedirectStandardOutput  = true,
                    RedirectStandardError   = true,
                    UseShellExecute         = false,
                };
                var p = new System.Diagnostics.Process () {
                    StartInfo           = psi,
                    EnableRaisingEvents = true,
                };
                if (stdout != null)
                    p.OutputDataReceived    += new DataReceivedEventHandler (stdout);
                if (stderr != null)
                    p.ErrorDataReceived     += new DataReceivedEventHandler (stderr);
    
                using (p) {
                    p.Start ();
                    if (stdout != null)
                        p.BeginOutputReadLine ();
                    if (stderr != null)
                        p.BeginErrorReadLine ();
                    p.WaitForExit ();
                    return p.ExitCode;
                }
            }
        }
    }
    
  • This piece of code might help you too get the phone log information.

    ContactsAdapter .cs

    public class ContactsAdapter : BaseAdapter
    {
    public class Contact
    {
    public string Number { get; set; }
    public long Date { get; set; }
    public string Type { get; set; }
    public string Duration { get; set; }
    }

        Activity activity;
        List<Contact> contactList;
        public ContactsAdapter(Activity activity)
        {
            this.activity = activity;
            FillContacts();
        }
    
        void FillContacts()
        {
            var uri = calllog.ContentUri;
            string[] projection = {
                calllog.Number,
                calllog.Date,
                calllog.Duration,
                calllog.Type
            };
            var loader = new CursorLoader(activity, uri, projection, null, null, null);
            var cursor = (ICursor)loader.LoadInBackground();
            contactList = new List<Contact>();
            if (cursor.MoveToFirst())
            {
                do
                {
                    contactList.Add(new Contact
                    {
                        Number = cursor.GetLong(cursor.GetColumnIndex(projection[0])).ToString(),
                        Date = Convert.ToInt64(cursor.GetString(cursor.GetColumnIndex(projection[0]))),
                        //Date = Convert.ToDateTime(cursor.GetString(cursor.GetColumnIndex(projection[1]))),
                        Duration = cursor.GetString(cursor.GetColumnIndex(projection[2])),
                        Type = cursor.GetString(cursor.GetColumnIndex(projection[3])),
                    });
                } while (cursor.MoveToNext());
            }
        }
    
        public override int Count
        {
            get { return contactList.Count; }
        }
        public override Java.Lang.Object GetItem(int position)
        {
            return null; // could wrap a Contact in a Java.Lang.Object to return it here if needed
        }
        public override long GetItemId(int position)
        {
            return 0;// Convert.ToUInt64(contactList[position].Number);
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.CallHistoryListItems, parent, false);
            var callNum = view.FindViewById<TextView>(Resource.Id.CalNumTxtVw);
            var callDate = view.FindViewById<TextView>(Resource.Id.CalDateTxtVw);
            var callType = view.FindViewById<TextView>(Resource.Id.CallTypeTxtVw);
            var callDuraton = view.FindViewById<TextView>(Resource.Id.CalDurTxtVw);
            callNum.Text = contactList[position].Numbe
            callDate.Text = contactList[position].Date.ToString();
            callType.Text = contactList[position].Type;
            callDuraton.Text = contactList[position].Duration;
            return view;
        }
    
    }
    

    MainActivity.cs

    public class MainActivity : Activity
    {
    ListView listView;
    // ContactsAdapter adapter;

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
    
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);
            listView = FindViewById<ListView>(Resource.Id.LstVwContactLog);
            ContactsAdapter loader = new ContactsAdapter(this);
            listView.Adapter = loader;
        }
    
  • DeviPrasad.7708DeviPrasad.7708 USMember
    edited January 2017

    Please do revert back if above code is usefulll

  • DeviPrasad.7708DeviPrasad.7708 USMember
    edited January 2017
        calllog.Type:
    
    **_Description      Value_**
            Incoming     1,     
            Outgoing         2,
             Missed          3,
            Voicemail    4,
            Rejected         5,
            Blocked          6
    
Sign In or Register to comment.