发送短信时如何获取信息?
How to get information when an SMS is send?
我想在用户 receives/sends 一条短信时得到信息。为此,我创建了一个 SmsWatcher class。
using AeroMobile.Utility.Helper;
using Android.App;
using Android.Content;
using Android.Provider;
[BroadcastReceiver]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED", "android.intent.action.SENDTO", "android.provider.Telephony.SMS_DELIVER" }, Priority = (int)IntentFilterPriority.HighPriority)]
public class SmsWatcher : BroadcastReceiver
{
public override async void OnReceive(Context context, Intent intent)
{
if (intent.Action != null)
{
if (intent.Action.Equals(Telephony.Sms.Intents.SmsReceivedAction))
{
var msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
foreach (var msg in msgs)
{
//incoming message
}
}
if (intent.Action.Equals(Telephony.Sms.Intents.SmsDeliverAction))
{
var msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
foreach (var msg in msgs)
{
//outgoing message
}
}
}
}
}
然后我在 MainActivity 中注册了这个广播服务,如下所示:
//starting SMS watcher service
var smsWatcherIntent = new Intent(ApplicationContext, typeof(SmsWatcher));
SendBroadcast(smsWatcherIntent);
以上代码非常适用于传入消息,但不适用于传出消息。当用户发送短信时,你能帮我获取信息吗?就像我收到的信息一样?
根据 this answer
的启发
从 BroadcastReceiver
读取外发消息似乎并不那么容易。因此,读取 SMS 的最佳方式是通过 Cursor 从 Android 数据库读取。
下面是 Xamarin.Android
的代码片段:
private async Task CheckAndUpdateSms(Context context)
{
string lastReadTimeMilliseconds = Application.Current.Properties["LastSmsReadMilliseconds"].ToString();
ICursor cursor = context.ContentResolver.Query(Telephony.Sms.ContentUri, null, "date > ?", new string[] { lastReadTimeMilliseconds }, null);
if (cursor != null)
{
int totalSMS = cursor.Count;
if (cursor.MoveToFirst())
{
double maxReceivedTimeMilliseconds = 0;
for (int j = 0; j < totalSMS; j++)
{
double smsReceivedTimeMilliseconds = cursor.GetString(cursor.GetColumnIndexOrThrow(Telephony.TextBasedSmsColumns.Date)).ToProperDouble();
string smsReceivedNumber = cursor.GetString(cursor.GetColumnIndexOrThrow(Telephony.TextBasedSmsColumns.Address));
int smsType = cursor.GetString(cursor.GetColumnIndexOrThrow(Telephony.TextBasedSmsColumns.Type)).ToProperInt();
//you can process this SMS here
if (maxReceivedTimeMilliseconds < smsReceivedTimeMilliseconds) maxReceivedTimeMilliseconds = smsReceivedTimeMilliseconds;
cursor.MoveToNext();
}
//store the last message read date/time stamp to the application property so that the next time, we can read SMS processed after this date and time stamp.
if (totalSMS > 0)
{
Application.Current.Properties["LastSmsReadMilliseconds"] = maxReceivedTimeMilliseconds.ToProperString();
}
}
cursor.Close();
}
}
我想在用户 receives/sends 一条短信时得到信息。为此,我创建了一个 SmsWatcher class。
using AeroMobile.Utility.Helper;
using Android.App;
using Android.Content;
using Android.Provider;
[BroadcastReceiver]
[IntentFilter(new[] { "android.provider.Telephony.SMS_RECEIVED", "android.intent.action.SENDTO", "android.provider.Telephony.SMS_DELIVER" }, Priority = (int)IntentFilterPriority.HighPriority)]
public class SmsWatcher : BroadcastReceiver
{
public override async void OnReceive(Context context, Intent intent)
{
if (intent.Action != null)
{
if (intent.Action.Equals(Telephony.Sms.Intents.SmsReceivedAction))
{
var msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
foreach (var msg in msgs)
{
//incoming message
}
}
if (intent.Action.Equals(Telephony.Sms.Intents.SmsDeliverAction))
{
var msgs = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
foreach (var msg in msgs)
{
//outgoing message
}
}
}
}
}
然后我在 MainActivity 中注册了这个广播服务,如下所示:
//starting SMS watcher service
var smsWatcherIntent = new Intent(ApplicationContext, typeof(SmsWatcher));
SendBroadcast(smsWatcherIntent);
以上代码非常适用于传入消息,但不适用于传出消息。当用户发送短信时,你能帮我获取信息吗?就像我收到的信息一样?
根据 this answer
的启发从 BroadcastReceiver
读取外发消息似乎并不那么容易。因此,读取 SMS 的最佳方式是通过 Cursor 从 Android 数据库读取。
下面是 Xamarin.Android
的代码片段:
private async Task CheckAndUpdateSms(Context context)
{
string lastReadTimeMilliseconds = Application.Current.Properties["LastSmsReadMilliseconds"].ToString();
ICursor cursor = context.ContentResolver.Query(Telephony.Sms.ContentUri, null, "date > ?", new string[] { lastReadTimeMilliseconds }, null);
if (cursor != null)
{
int totalSMS = cursor.Count;
if (cursor.MoveToFirst())
{
double maxReceivedTimeMilliseconds = 0;
for (int j = 0; j < totalSMS; j++)
{
double smsReceivedTimeMilliseconds = cursor.GetString(cursor.GetColumnIndexOrThrow(Telephony.TextBasedSmsColumns.Date)).ToProperDouble();
string smsReceivedNumber = cursor.GetString(cursor.GetColumnIndexOrThrow(Telephony.TextBasedSmsColumns.Address));
int smsType = cursor.GetString(cursor.GetColumnIndexOrThrow(Telephony.TextBasedSmsColumns.Type)).ToProperInt();
//you can process this SMS here
if (maxReceivedTimeMilliseconds < smsReceivedTimeMilliseconds) maxReceivedTimeMilliseconds = smsReceivedTimeMilliseconds;
cursor.MoveToNext();
}
//store the last message read date/time stamp to the application property so that the next time, we can read SMS processed after this date and time stamp.
if (totalSMS > 0)
{
Application.Current.Properties["LastSmsReadMilliseconds"] = maxReceivedTimeMilliseconds.ToProperString();
}
}
cursor.Close();
}
}