Read SMS messages from Android using .NET MAUI and platform code specific

AG 521 Reputation points
2023-02-16T13:31:00.1266667+00:00

Hi,

I would like to read SMS messages from Android so I use the Android code from here https://stackoverflow.com/a/66792224 but I am facing a problem with this line of code

var cursor = ContentResolver.Query(uri, reqCols, null, null, null);

that saying An object reference is required for the non-static field, method, or property 'ContentResolver.Query(Uri, string[]?, string?, string[]?, string?)'

Repo of the project is here https://github.com/asafgo/ReadSMSMessages

Thanks,

AG

Developer technologies .NET .NET MAUI
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AG 521 Reputation points
    2023-02-16T16:09:21.0566667+00:00

    Apparently needs to use Application Context

    var cursor = Android.App.Application.Context.ContentResolver.Query(uri, reqCols, null, null, null);
    
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.