【问题标题】:Xamarin C# Android Call LogsXamarin C# Android 通话记录
【发布时间】:2017-01-30 14:17:23
【问题描述】:

我在使用设备的此通话记录时遇到问题。 该代码根本没有返回日志,它给出了错误的数据。

代码:

    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”。 名字总是空白, 而且它总是一个拨出电话。

它给出了一个日期戳但不准确。

【问题讨论】:

  • 经过大量 db 搜索后,我发现这个 URI 只存储最近的数据,完整的历史记录没有存储在 URI 中,它在其他地方.. 修复了我的时间问题以及为什么 -1 但是我需要找到如何获取所有历史,网上所有的例子都没有涵盖这个

标签: c# android xamarin calllog


【解决方案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;
    }
}

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

【讨论】:

  • 添加一些解释,说明此答案如何帮助 OP 解决当前问题
【解决方案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;
    }

}

【讨论】:

  • 包括使用 calllog = Android.Provider.CallLog.Calls;在命名空间中
  • 添加一些解释,说明此答案如何帮助 OP 解决当前问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多