Hello,
can read all stored SMS on an android phone, can you give general directions for reading learning and implementing it in a .NET MAUI project?
Yes, before you read the SMS, you need to add following permissions in the <manifest>
tag of Platforms/Android/Resources/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:supportsRtl="true"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<!--add here-->
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-sdk />
</manifest>
Then you need to request permission at the runtime with Permissions - .NET MAUI | Microsoft Learn.
If you have created an empty MAUI project, you can open the Mainpages.xaml.cs
and copy following method to the MainPage class,
public async Task<PermissionStatus> CheckAndRequestSMSPermission()
{
PermissionStatus status = await Permissions.CheckStatusAsync<Permissions.Sms>();
if (status == PermissionStatus.Granted)
return status;
if (status == PermissionStatus.Denied && DeviceInfo.Platform == DevicePlatform.iOS)
{
// Prompt the user to turn on in settings
// On iOS once a permission has been denied it may not be requested again from the application
return status;
}
if (Permissions.ShouldShowRationale<Permissions.Sms>())
{
// Prompt the user with additional information as to why the permission is needed
}
status = await Permissions.RequestAsync<Permissions.Sms>();
return status;
}
Next, you can read SMS with ContentResolver
for Android, I get the sms in the button click for testing. You can refer to the following code. You can open the Mainpages.xaml.cs
and find OnCounterClicked
method, delete code in the OnCounterClicked
, copy following method to the OnCounterClicked method,
private async void OnCounterClicked(object sender, EventArgs e)
{
var res= await CheckAndRequestSMSPermission();
if (res.Equals( PermissionStatus.Granted))
{
#if ANDROID
List<string> items=new List<string>();
string INBOX = "content://sms/inbox";
string[] reqCols = new string[] { "_id", "thread_id", "address", "person", "date", "body", "type" };
Android.Net.Uri uri = Android.Net.Uri.Parse(INBOX);
Android.Database.ICursor cursor = Microsoft.Maui.ApplicationModel.Platform.CurrentActivity.ContentResolver.Query(uri, reqCols, null, null, null);
if (cursor.MoveToFirst())
{
do
{
string messageId = cursor.GetString(cursor.GetColumnIndex(reqCols[0]));
string threadId = cursor.GetString(cursor.GetColumnIndex(reqCols[1]));
string address = cursor.GetString(cursor.GetColumnIndex(reqCols[2]));
string name = cursor.GetString(cursor.GetColumnIndex(reqCols[3]));
string date = cursor.GetString(cursor.GetColumnIndex(reqCols[4]));
string msg = cursor.GetString(cursor.GetColumnIndex(reqCols[5]));
string type = cursor.GetString(cursor.GetColumnIndex(reqCols[6]));
items.Add(messageId + (","
+ (threadId + (","
+ (address + (","
+ (name + (","
+ (date + (" ,"
+ (msg + (" ," + type))))))))))));
} while (cursor.MoveToNext());
}
#endif
}
}
In the end, you can start debug your application in android emulator or devices, click the CounterBtn button, all of the sms messages will store in the List<string> items=new List<string>();
.
Best Regards,
Leon Lu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.