Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

Trying to create an IntentService, but facing problems

$
0
0

Hi,

I took the sample StockService as an example to create a service within a Forms app, but basically the problem is that OnHandleIntent is never called, although the in the IServiceConnection the OnServiceConnected is being executed.

In the StockService example, when creating the Intent to start the service its like this:
Intent notificationServiceIntent = new Intent ("com.xamarin.StockService"); The "com.xamarin.StockService" is the same as in the service's IntentFilter's string.

But this throws an exception just a couple of lines later when I call this:
Forms.Context.BindService (notificationServiceIntent, notificationServiceConnection, Bind.AutoCreate); The exception is saying the Intent must be explicit.
The only way to not to get an exception is to create the intent like this:
Intent notificationServiceIntent = new Intent (Forms.Context, typeof(BeaconNotificationService)); where BeaconNotificationService is the service's class name.
But doing this way OnHandleIntent is not called.

For the sake of completeness here is the code of the service:

(

using System;
using Xamarin.Forms;
using Namespace.Droid;
using Android.App;
using Android.Content;
using Android.OS;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
[assembly: Dependency (typeof (BeaconNotificationService))]
namespace Namespace.Droid
{
    [Service]
    [IntentFilter(new String[]{"some.namespace.NotificationService"})]
    public class BeaconNotificationService : IntentService, INotificationService
    {
        public IBinder binder;
        public bool isBound = false;
        Intent notificationServiceIntent;
        NotificationServiceReceiver broadcastReceiver;
        NotificationServiceConnection notificationServiceConnection;
        public BeaconNotificationService ()
        {
        }
        public void StartNotificationService()
        {
            notificationServiceIntent = new Intent (Forms.Context, typeof(BeaconNotificationService));
            notificationServiceIntent.SetAction ("ch.elopsis.NotificationService");
            broadcastReceiver = new NotificationServiceReceiver ();
            var intentFilter = new IntentFilter (CustomBeaconService.BeaconsUpdatedAction){Priority = (int)IntentFilterPriority.HighPriority};
            Forms.Context.RegisterReceiver (broadcastReceiver, intentFilter);
            notificationServiceConnection = new NotificationServiceConnection (this);
            Forms.Context.BindService (notificationServiceIntent, notificationServiceConnection, Bind.AutoCreate);
        }
        public void StopNotificationService()
        {
            if (isBound) {
                Forms.Context.UnbindService (notificationServiceConnection);
                isBound = false;
            }
            Forms.Context.UnregisterReceiver (broadcastReceiver);
        }
        public override IBinder OnBind (Intent intent)
        {
            binder = new NotificationServiceBinder (this);
            return binder;
        }
        #region implemented abstract members of IntentService
        protected override void OnHandleIntent (Android.Content.Intent intent)
        {
            //Not getting called
        }
        #endregion
    }
    public class NotificationServiceBinder : Binder
    {
        BeaconNotificationService service;
        public NotificationServiceBinder (BeaconNotificationService service)
        {
            this.service = service;
        }
        public BeaconNotificationService GetBeaconService ()
        {
            return service;
        }
    }
    [BroadcastReceiver]
    class NotificationServiceReceiver : BroadcastReceiver
    {
        public override void OnReceive (Context context, Android.Content.Intent intent)
        {
            InvokeAbortBroadcast();
        }
    }
    class NotificationServiceConnection : Java.Lang.Object, IServiceConnection
    {
        BeaconNotificationService instance;
        public NotificationServiceConnection (BeaconNotificationService instance)
        {
            this.instance = instance;
        }
        public void OnServiceConnected (ComponentName name, IBinder service)
        {
            var notificationServiceBinder = service as NotificationServiceBinder;
            if (notificationServiceBinder != null) {
                var binder = (NotificationServiceBinder)service;
                instance.binder = binder;
                instance.isBound = true;
            }
        }
        public void OnServiceDisconnected (ComponentName name)
        {
            instance.isBound = false;
        }
    }
}
)

Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>