我想创建一个服务来“启动” SMSReceived Broadcastreceiver吗? 如何在Xamarin Forms(尤其是Android)中做到这一点?
我更改了以下代码以解决我的问题。
这是Android原生代码。
我在主项目的“主要活动”中使用DependendyServices调用此类。
由“ SMSHelper”的“ CheckForIncommingSMS”方法创建的Broadcastreceiver正常工作(在应用程序运行时也被调用)。
using Android.App;
using Android.Content;
using Android.OS;
using Android.Util;
using System;
[assembly: Xamarin.Forms.Dependency(typeof(NAMESPACE.BackgroundServiceHelper))]
namespace NAMESPACE
{
[Service(Name = "APPNAME.BackgroundService")]
class BackgroundServiceHelper : Service, IBackgroundServiceHelper
{
static readonly string TAG = typeof(BackgroundServiceHelper).FullName;
static readonly int DELAY_BETWEEN_LOG_MESSAGES = 5000; // milliseconds
DateTime timestamper;
bool isStarted;
Handler handler;
Action runnable;
public override void OnCreate()
{
base.OnCreate();
Log.Info(TAG, "OnCreate: the service is initializing.");
timestamper = new DateTime();
handler = new Handler();
runnable = new Action(() =>
{
if (timestamper != null)
{
Log.Debug(TAG, DateTime.UtcNow.ToLongDateString());
SMSHelper smsh = new SMSHelper();
// Method wich registers the SMSReceived BrodcastReceiver
smsh.CheckForIncommingSMS();
handler.PostDelayed(runnable, DELAY_BETWEEN_LOG_MESSAGES);
}
});
}
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
{
if (isStarted)
{
Log.Info(TAG, "OnStartCommand: This service has already been started.");
}
else
{
Log.Info(TAG, "OnStartCommand: The service is starting.");
DispatchNotificationThatServiceIsRunning();
handler.PostDelayed(runnable, DELAY_BETWEEN_LOG_MESSAGES);
isStarted = true;
}
// This tells Android not to restart the service if it is killed to reclaim resources.
return StartCommandResult.NotSticky;
}
public override IBinder OnBind(Intent intent)
{
// Return null because this is a pure started service. A hybrid service would return a binder that would
return null;
}
public override void OnDestroy()
{
// We need to shut things down.
Log.Info(TAG, "OnDestroy: The started service is shutting down.");
// Stop the handler.
handler.RemoveCallbacks(runnable);
//timestamper = null;
isStarted = false;
base.OnDestroy();
}
void DispatchNotificationThatServiceIsRunning()
{
}
}
}
答案 0 :(得分:2)
在本地android中创建此类
namespace MAMN.Droid.Native
{
[BroadcastReceiver]
[IntentFilter(new string[] { "android.provider.Telephony.SMS_RECEIVED" }, Priority =
Int32.MaxValue)]
public class MessageBoard : BroadcastReceiver
{
SmsMessage[] messages;
public static readonly string INTENT_ACTION = "android.provider.Telephony.SMS_RECEIVED";
public override void OnReceive(Context context, Intent intent)
{
try
{
if (intent.Action != INTENT_ACTION) return;
messages = Telephony.Sms.Intents.GetMessagesFromIntent(intent);
ManageSMS();
}
catch (Exception) { }
}
public void ManageSMS()
{
var dd = messages[0].DisplayMessageBody.ToString();
string msg = new String(dd.Where(Char.IsDigit).ToArray());
//this is the message center i have subscribe to get the message text in my pc
MessagingCenter.Send<object, string>(this, "OTP", msg);
}
}
}
然后参加主要活动
MessageBoard SMSReceiver = new MessageBoard();
var smsFilter = new
IntentFilter("android.provider.Telephony.SMS_RECEIVED")
{
Priority =
(int)IntentFilterPriority.HighPriority
};
RegisterReceiver(SMSReceiver, smsFilter);
还要在清单中给出许可 希望对您有帮助