Reputation: 41
I have a background service that initializes the System.Net.MQTT library and wait the message from mqtt . I have a ptoblem the OnHandleIntent inizialize library and close service!! at boot startup , and not receiver the message , Why??
[BroadcastReceiver(Label = "StartReceiver", Enabled = true)] [IntentFilter(new[] { Intent.ActionBootCompleted })] public class StartReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { Toast.MakeText(context, "i got it " , ToastLength.Long).Show(); if (intent.Action == Intent.ActionBootCompleted) { var serviceIntent = new Intent(context, typeof(ServiceTermoCoperta)); serviceIntent.AddFlags(ActivityFlags.NewTask); context.StartService(serviceIntent); } } } [Service(Exported = true, Enabled = true)] public class ServiceTermoCoperta : IntentService { public IMqttClient clientMQTT; [return: GeneratedEnum] public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId) { base.OnStartCommand( intent, flags, startId); return StartCommandResult.Sticky; } protected override async void OnHandleIntent(Intent intent) { var mqttConfig = new MqttConfiguration { Port = 1883, MaximumQualityOfService = MqttQualityOfService.ExactlyOnce, KeepAliveSecs = 60, //WaitTimeoutSecs = 50, //ConnectionTimeoutSecs = 50, AllowWildcardsInTopicFilters = true }; clientMQTT = await MqttClient.CreateAsync("iot.pushetta.com", mqttConfig); new Handler(Looper.MainLooper).Post(() => { if (clientMQTT != null) { clientMQTT.ConnectAsync(new MqttClientCredentials("pusmdm476u47r", "xxxxxx", "aaaaaa")).Wait(); clientMQTT.SubscribeAsync("/pushetta.com/channels/tteste", MqttQualityOfService.AtLeastOnce).Wait(); clientMQTT.MessageStream.Subscribe(msg => { string bs = msg.Topic + " " + Encoding.Default.GetString(msg.Payload); //Send Data Intent localIntent = new Intent(Constants.BROADCAST_ACTION).PutExtra(Constants.EXTENDED_DATA_STATUS, bs); // Broadcasts the Intent to receivers in this app. SendBroadcast(localIntent); }); } }); } }
Upvotes: 0
Views: 192
Reputation: 4358
IntentService
belong to calculate service which you can use it to complete work that will take much time.
Service
IntentService
will open another worker thread which is different from UI thread to complete work.
IntentService
will stop by itselt when the work is finished.
So I suggest you use Service
to achieve your goal.
Upvotes: 1