ServiceConnectionクラスでOnServiceConnectedイベントを試してみました。 「VAR myObjectという= IBinderObject myBinderClassとして、」私はそのようにしてくださいしようとした場合でも、(IBinderを継承し、それはバインダーを継承し、)私のクラスにIBinderをキャストすると、nullを返します私は「するvar myObjectという=(myBinderClass)IBinderObject;」として、それを実行しようとした場合、またはエラーをキャスト。 してください、任意の作業例...Mono(Xamarin)でAccessibilityServiceにバインドする方法
MainActivityコード:
namespace MyApp.Droid
{
[Activity(Label = "MyApp", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity
{
AccessibilityServiceBinder binder;
AccessibilityServiceConnection accessibilityServiceConnection;
bool isBound = false, isBoundAC = false, isConfigurationChange = false;
protected Intent ServiceIntent;
AccessibilityServiceClass ServiceClass;
protected override void OnStart()
{
base.OnStart();
if (!isBoundAC)
{
accessibilityServiceConnection = new AccessibilityServiceConnection(this);
isBoundAC = BindService(ServiceIntent, accessibilityServiceConnection, Bind.AutoCreate);
if (isBoundAC)
{
Toast.MakeText(this, "AccessibilityService is Bound", ToastLength.Long);
}
}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
ServiceIntent = new Intent(this, typeof(AccessibilityServiceClass));
ServiceIntent.SetPackage("android.accessibilityservice.AccessibilityService");
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
public class AccessibilityServiceConnection : Java.Lang.Object, IServiceConnection
{
MainActivity activity;
AccessibilityServiceBinder binder;
public AccessibilityServiceBinder Binder
{
get
{
return (AccessibilityServiceBinder)binder;
}
}
public AccessibilityServiceConnection(MainActivity activity)
{
this.activity = activity;
}
public void OnServiceConnected(ComponentName name, IBinder service)
{
var accessibilityServiceBinder = service as AccessibilityServiceBinder;
//accessibilityServiceBinder is always null here
if (accessibilityServiceBinder != null)
{
activity.binder = accessibilityServiceBinder;
activity.isBound = true;
this.binder = accessibilityServiceBinder;
}
}
public void OnServiceDisconnected(ComponentName name)
{
activity.isBound = false;
}
}
public override Java.Lang.Object OnRetainNonConfigurationInstance()
{
base.OnRetainNonConfigurationInstance();
isConfigurationChange = true;
return accessibilityServiceConnection;
}
}
}
サービスコード:
namespace MyApp.Droid
{
[Service(Label = "MyApp", Permission = Manifest.Permission.BindAccessibilityService), IntentFilter(new[] { "android.accessibilityservice.AccessibilityService" }), MetaData("android.accessibility-service", Resource = "@xml/accessibilityserviceconfig")]
public class AccessibilityServiceClass : Android.AccessibilityServices.AccessibilityService
{
public override void OnAccessibilityEvent(AccessibilityEvent e)
{
Toast.MakeText(this, "OnAccessibilityEvent", ToastLength.Short);
}
public override void OnInterrupt()
{
Toast.MakeText(this, "OnInterrupt", ToastLength.Short);
}
protected override void OnServiceConnected()
{
Toast.MakeText(this, "OnServiceConnected", ToastLength.Short);
}
}
public class AccessibilityServiceBinder : Binder
{
AccessibilityServiceClass service;
public AccessibilityServiceBinder(AccessibilityServiceClass service)
{
this.service = service;
}
public AccessibilityServiceClass GetAccessibilityService()
{
return service;
}
}
}
が完了あなたのサービスコード – Gusman
を追加...ありがとう。 – Gambolt