我正在开发一个 .NET MAUI 应用程序,我需要从用户的设备获取呼叫日志。在 Android 和 iOS 中。并在我的应用程序中显示来电者姓名和通话时长。
我为Android编写了以下代码,但在尝试查询通话日志时遇到错误。
如何解决与 Android.App.Application.Context.ContentResolver.Query 相关的错误?
是否有更好的方法在 Android 版 .NET MAUI 中获取通话日志?
iOS 有没有更好的方法来处理通话相关功能?
public IEnumerable<CallLogEntry> GetCallLogs()
{
var callLogs = new List<CallLogEntry>();
var uri = CallLog.Calls.ContentUri;
string[] projection = {
CallLog.Calls.Number,
CallLog.Calls.Type,
CallLog.Calls.Date,
CallLog.Calls.Duration,
CallLog.Calls.CachedName
};
using (ICursor cursor = Android.App.Application.Context.ContentResolver.Query(uri, projection, null, null, CallLog.Calls.Date + " DESC"))
{
if (cursor.MoveToFirst())
{
do
{
var number = cursor.GetString(cursor.GetColumnIndex(CallLog.Calls.Number));
var type = (CallType)cursor.GetInt(cursor.GetColumnIndex(CallLog.Calls.Type));
var date = new DateTime(cursor.GetLong(cursor.GetColumnIndex(CallLog.Calls.Date)));
var duration = cursor.GetLong(cursor.GetColumnIndex(CallLog.Calls.Duration));
var name = cursor.GetString(cursor.GetColumnIndex(CallLog.Calls.CachedName));
callLogs.Add(new CallLogEntry
{
Number = number,
Type = type,
Date = date,
Duration = duration,
Name = name
});
} while (cursor.MoveToNext());
}
}
return callLogs;
}
**Issue**
I am getting the following error:
Error: 'Android.App.Application' is not found. Are you missing any reference assembly?
**What I've Tried**
I have the necessary permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
Tried cleaning and rebuilding the project.
我拥有必要的权限 AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
对于Android平台,您可以使用
global :: Android.App.Application.Context.ContentResolver.Query
获取通话记录。因为是基于Platform.Android,所以需要添加global ::
。
对于IOS平台,由于苹果政策,您不能这样做。首先Apple官方并没有公开任何公共API来访问调用日志。