Xamarin C# Android 通话记录 [英] Xamarin C# Android Call Logs

查看:39
本文介绍了Xamarin C# Android 通话记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在处理此设备的通话记录时遇到问题.代码根本没有返回日志,它提供了错误的数据.

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

代码:

    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{


        }
    }

数字总是-1".名字总是空白并且它总是一个去电.

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

它提供了日期戳但不准确.

It gives a datestamp but not accurate.

推荐答案

//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;
    }
}

以上代码可能对您有所帮助.我也是 Xamarin 的新手,上面的代码我适当的日期时间.如果您有任何疑问,请告诉我.

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.

这篇关于Xamarin C# Android 通话记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆