Xamarin C# Android Call Logs

徘徊边缘 提交于 2021-02-20 04:34:42

问题


Im having trouble with this call logs of the device. The code is not returning the logs at all, It's giving false data.

Code:

    public void ReadCalls()
    {
        try {
            Android.Content.Context myContext = Android.App.Application.Context;
            string myDateToCheck = myServiceRef.myTimeToCheck("CallLog");
            if (myDateToCheck==null || myDateToCheck=="")
            {myDateToCheck = "0";}

            ICursor cursor = myContext.ContentResolver.Query(  Android.Net.Uri.Parse ("content://call_log/calls"), null, "Date > ?", new string[]{myDateToCheck }, "Date ASC");
            if (cursor.MoveToFirst ()) {
                while (!cursor.IsAfterLast ) {
                    if (cursor.GetLong (cursor.GetColumnIndex (CallLog.Calls.Date)) > long.Parse (myDateToCheck)) {
                        string Number = cursor.GetString (cursor.GetColumnIndex (CallLog.Calls.Number));
                        string Name = cursor.GetString (cursor.GetColumnIndex (CallLog.Calls.CachedName));
                        if (Name == null || Name == "") {
                            Name = "Unknown";
                        }
                        string Date = cursor.GetString (cursor.GetColumnIndex (CallLog.Calls.Date));
                        string Duration = cursor.GetString (cursor.GetColumnIndex (CallLog.Calls.Duration));
                        string Type = cursor.GetString (cursor.GetColumnIndex (CallLog.Calls.Type));
                        if (Number == "0") {
                            Number = "Unknown";
                        } else if (Number == "-1") {
                            Number = "Unknown";
                        }
                        long num = long.Parse (Date);
                        Java.Text.SimpleDateFormat simpleDateFormat = new Java.Text.SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
                        DateTime dateTime = new DateTime (1970, 1, 1).AddMilliseconds ((double)num);
                        dateTime = dateTime.AddMilliseconds (simpleDateFormat.TimeZone.GetOffset (num));
                        if (Type == "1") {
                            Type = "Outgoing";
                        } else if (Type == "2") {
                            Type = "Incoming";
                        } else if (Type == "3") {
                            Type = "Missed Call";
                        }
                        // now need to write it to a database 


                        MyCallLog  myLine = new MyCallLog {
                            TheNumber  = Number ,
                            TheName = Name ,
                            TheTime = dateTime.ToString() ,
                            TheDirection  = Type ,
                            TheDuration  = Duration 
                        };
                        string output = Newtonsoft.Json.JsonConvert.SerializeObject (myLine );
                        myServiceRef.myDatabaseConnection (output);
                    } else {
                        break;
                    }
                    cursor.MoveToNext ();
                }
            }

        }catch{


        }
    }

The number is always "-1". The name is always blank, and its always an outgoing call.

It gives a datestamp but not accurate.


回答1:


//static class to convert milisecond to datetime    
static class ConvertToDate
{
    static readonly DateTime UnixEpochStart = DateTime.SpecifyKind(new DateTime(1970, 1, 1), DateTimeKind.Utc); //Coverting date in to universal

    public static DateTime ToDateTimeFromEpoch(this long epochTime)
    {
        DateTime result = UnixEpochStart.AddMilliseconds(epochTime);
        return result;
    }
}

Above code might help you. I am also new to Xamarin and above code gives me the appropriate date time. Please let me know if you have any queries.




回答2:


 public class ContactsAdapter : BaseAdapter
{
    Activity activity;
    List<Contact> contactList;
    public ContactsAdapter(Activity activity)
    {
        this.activity = activity;
        FillContacts();
    }
    void FillContacts()
    {
        var uri = calllog.ContentUri;
        //var uri = ContactsContract.Contacts.ContentUri;
        string[] projection = {
            calllog.Number,
            calllog.Date,
            calllog.Duration,
            calllog.Type,
            calllog.CachedName,
            calllog.CachedPhotoId
        };
        // CursorLoader introduced in Honeycomb (3.0, API11)
        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.GetString(cursor.GetColumnIndex(projection[0])),
                    Date = cursor.GetLong(cursor.GetColumnIndex(projection[1])),
                    Duration = cursor.GetString(cursor.GetColumnIndex(projection[2])),
                    Type = cursor.GetString(cursor.GetColumnIndex(projection[3])),
                    Name = cursor.GetString(cursor.GetColumnIndex(projection[4])),
                    PhotoId = cursor.GetString(cursor.GetColumnIndex(projection[5]))
                });
            } while (cursor.MoveToNext());
        }
    }
    public override int Count
    {
        get { return contactList.Count; }
    }
    public override Java.Lang.Object GetItem(int position)
    {
        return null;
    }
    public override long GetItemId(int position)
    {
        return 0;
    }
    public override View GetView(int position, View convertView, ViewGroup parent)
    {
        var view = convertView ?? activity.LayoutInflater.Inflate(Resource.Layout.CallLogItems, parent, false);
        var callNum = view.FindViewById<TextView>(Resource.Id.NumTxtVw);
        var callType = view.FindViewById<ImageView>(Resource.Id.TypeIndicatImgVw);
        var calldate = view.FindViewById<TextView>(Resource.Id.CallTime);
        var name = view.FindViewById<TextView>(Resource.Id.CallerNameTxtVw);
        var contactImg = view.FindViewById<ImageView>(Resource.Id.ContactImgVw);

        callNum.Text = contactList[position].Number;

        calldate.Text = ConvertToDate.ToDateTimeFromEpoch(contactList[position].Date).ToString();// ToDateTimeFromEpoch(contactList[position].Date).ToString();
        if (string.IsNullOrWhiteSpace(contactList[position].Name))
        {
            name.Text = "Unkown";
        }
        else
        {
            name.Text = contactList[position].Name;

        }
        if (contactList[position].PhotoId == null)
        {
            contactImg = view.FindViewById<ImageView>(Resource.Id.ContactImgVw);
            contactImg.SetImageResource(Resource.Drawable.contactimg);
        }
        else
        {
            contactImg = view.FindViewById<ImageView>(Resource.Id.ContactImgVw);
            contactImg.SetImageResource(Resource.Drawable.contactimg);

        }
        if (contactList[position].Type == "1")
        {
            var contactImage = view.FindViewById<ImageView>(Resource.Id.TypeIndicatImgVw);
            contactImage.SetImageResource(Resource.Drawable.incoming);

        }
        else
        if (contactList[position].Type == "2")
        {
            var contactImage = view.FindViewById<ImageView>(Resource.Id.TypeIndicatImgVw);
            contactImage.SetImageResource(Resource.Drawable.outgoing);
        }
        else
        if (contactList[position].Type == "3")
        {
            var contactImage = view.FindViewById<ImageView>(Resource.Id.TypeIndicatImgVw);
            contactImage.SetImageResource(Resource.Drawable.misssedcall);
        }
        else
        if (contactList[position].Type == "4")
        {
            var contactImage = view.FindViewById<ImageView>(Resource.Id.TypeIndicatImgVw);
            contactImage.SetImageResource(Resource.Drawable.voicemail);
        }

        else
        if (contactList[position].Type == "5")
        {
            var contactImage = view.FindViewById<ImageView>(Resource.Id.TypeIndicatImgVw);
            contactImage.SetImageResource(Resource.Drawable.reject);
        }
        else
        if (contactList[position].Type == "6")
        {
            var contactImage = view.FindViewById<ImageView>(Resource.Id.TypeIndicatImgVw);
            contactImage.SetImageResource(Resource.Drawable.blocked);
        }
        return view;
    }

}


来源:https://stackoverflow.com/questions/37542452/xamarin-c-sharp-android-call-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!