如何在另一个项目Xamarin项目中包含BroadcastReceiver? [英] How to include a BroadcastReceiver in a different project Xamarin project?

查看:208
本文介绍了如何在另一个项目Xamarin项目中包含BroadcastReceiver?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Xamarin中编写一个跨平台的解决方案,并且即将添加一个用于3rd-partyu集成的母项目(例如,其他仅希望使用Android上的意图与我的应用集成的Android缔约方)。

I'm writing a cross-platform solution in Xamarin, and am about to add a mother project for 3rd-partyu integration (e.g., other Android-parties that want to integrate with my app using intents on Android only).

但是我想保留我现在已经干净的跨平台设置,只需添加带有第3方集成代码的新Android类库即可。但是,我似乎无法在我的第3方集成项目中将自己的BroadcastReceiver包含在主要 Android应用程序项目中。

But I'd like to keep my cross-platform setup that I have now "clean" and simply add a new Android class library with the 3rd-party integration code. However, I cannot seem to get my BroadcastReceiver in my 3rd-party integration project to be included in my "main" android app project.

我已经添加了对第三方集成项目,但这显然还不够……我可以将我的BroadcastReceiver添加到主要的Android应用程序项目中,我猜这一切都很好,但我真的很想保留第三方集成项目党的东西孤立了,因为我可以很好地想象将来为其他目的添加其他集成机制,然后也希望将它们保留在另一个项目中。

I've added a reference to the 3rd-party integration project, but that's obviously not sufficient... I could just add my BroadcastReceiver in the main Android-app project instead, and that would be all well I guess, but I'd really like to keep the 3rd-party stuff isolated, since I could very well imaging adding other integration mechanisms in the future for other purposes and would then like to keep those in yet another project too.

任何建议?目前,在编译主要android-project时,第3方项目似乎甚至没有自动编译。

Any suggestions? Currently, the 3rd-party project does not even seem to get compiled automatically when compiling the main android-project.

编辑;

我的BroadcastReceiver使用属性推荐;

My BroadcastReceiver uses attributes as "recommended";

[BroadcastReceiver(Enabled = true)]
[IntentFilter(new[] { "se.millsys.integration.MyIntegrationIntent" })]
public class IntegrationBroadcastReceiver : BroadcastReceiver
{
    private static string DEBUG_TAG = "IntegrationBroadcastReceiver";

    public override void OnReceive(Context context, Intent intent)
    {
    }
}


推荐答案

假设设置如下:



Assuming this setup:

├── DroidMainApp
│   └── MyBroadcastReceiver (A Project level reference to DroidMainApp)
├── ADifferentApp



DroidMainApp




  • 创建的Android应用程序模板

  • 包名称的 com.sushihangover.toaster

  • DroidMainApp

    • A template created Android app
    • Package name of com.sushihangover.toaster

      • 创建的Android库项目模板

      • 包含一个C#文件/类

      • BroadcastReceiver是名为 com.sushihangover.toaster.receiver

      • BroadcastReceiver已启用 已导出

      • Template created Android Library Project
      • Contains one C# file/class
      • BroadcastReceiver is named com.sushihangover.toaster.receiver
      • BroadcastReceiver is Enabled and Exported
      [BroadcastReceiver(Name = "com.sushihangover.toaster.receiver", Enabled = true, Exported = true)]
      public class Toaster : BroadcastReceiver
      {
          public override void OnReceive(Context context, Intent intent)
          {
              Toast.MakeText(context, "Go make some toast", ToastLength.Long).Show();
              Log.Info("SO", "Go make some toast");
          }
      }
      



      另一个应用程序调用DroidMainApp的 BroadcastReceiver




      • 使用明确的意图

      • A Different App calling DroidMainApp's BroadcastReceiver:

        • Using an Explicit Intent
        • var toasterIntent = new Intent();
          toasterIntent.SetClassName("com.sushihangover.toaster", "com.sushihangover.toaster.receiver");
          var pendingIntent = PendingIntent.GetBroadcast(this, 0, toasterIntent, PendingIntentFlags.CancelCurrent);
          var alarmService = (AlarmManager)GetSystemService(Context.AlarmService);
          alarmService.SetRepeating(AlarmType.Rtc, 1000, 60000, pendingIntent);
          

          这篇关于如何在另一个项目Xamarin项目中包含BroadcastReceiver?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆