如何在不同的项目 Xamarin 项目中包含 BroadcastReceiver? [英] How to include a BroadcastReceiver in a different project Xamarin project?

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

问题描述

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

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).

但我想保持我现在干净"的跨平台设置,只需添加一个带有 3rd 方集成代码的新 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.

我已经添加了对 3rd-party 集成项目的引用,但这显然还不够......我可以在主 Android 应用项目中添加我的 BroadcastReceiver,我想这一切都很好,但是我真的很想将第 3 方的东西隔离开来,因为我可以很好地想象将来为其他目的添加其他集成机制,然后也希望将它们保留在另一个项目中.

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 时,3rd-party 项目似乎甚至没有自动编译.

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)
    {
    }
}

推荐答案

假设这个设置:

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

DroidMainApp

  • 模板创建的 Android 应用
  • com.sushihangover.toaster 的包名
    • 模板创建的 Android 库项目
    • 包含一个 C# 文件/类
    • BroadcastReceiver 被命名为 com.sushihangover.toaster.receiver
    • BroadcastReceiver 是 EnabledExported
    [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 的不同应用:

    • 使用显式 Intent
    • 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天全站免登陆