Reputation: 3
I am developing a .NET MAUI application and I need to fetch the call logs from a user's device. in Android and iOS . and show the caller name and duration of the call in my app .
I have written the following code for Android, but I am encountering an error when trying to query the call logs.
How can I resolve the error related to Android.App.Application.Context.ContentResolver.Query?
Is there a better way to fetch call logs in .NET MAUI for Android?
Is there any better way for iOS to handle call-related features?
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.
Upvotes: 0
Views: 429
Reputation: 4521
I have the necessary permissions in AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_CALL_LOG" />
For the Android platform, you can use the global :: Android.App.Application.Context.ContentResolver.Query
to get the call logs. Because it based on the Platform.Android, you need to add the global ::
.
For the IOS platform, you can not do this due to the apple policy. Apple officially does not expose any public API to access the call log. You can check ths link about the CallKit.
Upvotes: 0