问题
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).
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.
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.
Any suggestions? Currently, the 3rd-party project does not even seem to get compiled automatically when compiling the main android-project.
EDIT;
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)
{
}
}
回答1:
Assuming this setup:
├── DroidMainApp
│ └── MyBroadcastReceiver (A Project level reference to DroidMainApp)
├── ADifferentApp
DroidMainApp
- A template created Android app
- Package name of
com.sushihangover.toaster
MyBroadcastReceiver
- Template created Android Library Project
- Contains one C# file/class
- BroadcastReceiver is named
com.sushihangover.toaster.receiver
- BroadcastReceiver is
Enabled
andExported
[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");
}
}
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);
来源:https://stackoverflow.com/questions/39324343/how-to-include-a-broadcastreceiver-in-a-different-project-xamarin-project