2016-05-31 91 views
0

我的设备的通话记录有问题。 代码根本没有返回日志,它给出了错误的数据。Xamarin C#Android通话记录

代码:

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”。 该名称始终为空, 且始终为传出呼叫。

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

+0

DB搜索的很多之后,我发现这个URI只存储最新的数据,完整的历史并不存储在URI,它在其他地方..固定我的时间问题,为什么-1,但我需要找到如何获取所有的历史记录,所有在网上的例子不包括这 – Migz

回答

0
//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及以上代码给出了我适当的日期时间。如果您有任何疑问,请让我知道。

+0

添加一些解释和回答这个答案如何帮助OP在解决当前问题 –

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

} 
+1

包括使用calllog = Android.Provider.CallLog.Calls;在命名空间 –

+0

添加一些解释并回答这个答案如何帮助OP解决当前问题 –