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

那年仲夏 提交于 2019-11-29 18:16:36

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!