How to include a BroadcastReceiver in a different project Xamarin project?

后端 未结 1 1412
梦如初夏
梦如初夏 2020-12-22 08:00

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 m

相关标签:
1条回答
  • 2020-12-22 08:37

    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 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");
        }
    }
    

    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);
    
    0 讨论(0)
提交回复
热议问题