android - Xamarin/Android 应用程序 : Broadcast receiver not working

标签 android xamarin xamarin.android broadcastreceiver

我试图在拨出电话时显示通知,但它不起作用(未显示任何内容)。我单独测试了通知,它工作正常,但我很确定我在 BroadcastReceiver 中做错了什么。 提前致谢!

主要 Activity :

[Activity(Label = "ExamProject", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        MyBroadcastRecieverClass reciever;
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        reciever = new MyBroadcastRecieverClass();
        RegisterReceiver(reciever, new IntentFilter("android.intent.action.NEW_OUTGOING_CALL"));

广播接收者类:

namespace App3
{
[BroadcastReceiver(Enabled = true, Exported = true)]
[IntentFilter(new[] { Android.Content.Intent.ActionNewOutgoingCall })]
public class MyBroadcastRecieverClass: BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        // Do stuff here.

        //String value = intent.GetStringExtra("key");

        Notification.Builder builder = new Notification.Builder(Application.Context)
              .SetContentTitle("Test Title")
              .SetContentText("test content")
              .SetSmallIcon(Resource.Drawable.Icon)
              .SetPriority(100)
              .SetDefaults(NotificationDefaults.Sound)
              .SetVisibility(NotificationVisibility.Public)
              .SetCategory(Notification.CategoryAlarm);


        //build the notification
        Notification test = builder.Build();

        // Get the notification manager:
        NotificationManager notificationManager =
            context.GetSystemService(Context.NotificationService) as NotificationManager;

        // Publish the notification:
        const int notificationId = 0;//should be changed to generate a uniqe value
        notificationManager.Notify(notificationId, test);
    }
}

最佳答案

我发现了错误,这里有语法问题:

RegisterReceiver(reciever, new IntentFilter("android.intent.action.NEW_OUTGOING_CALL"));

正确的代码:

public class MainActivity : Activity
{
    protected override void OnCreate(Bundle bundle)
    {
        MyBroadcastRecieverClass reciever;
        base.OnCreate(bundle);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);
        reciever = new MyBroadcastRecieverClass();

        IntentFilter myIntentFilter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
        RegisterReceiver(reciever, myIntentFilter);

        Button settingsButton = FindViewById<Button>(Resource.Id.settingsButton);

关于android - Xamarin/Android 应用程序 : Broadcast receiver not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43192451/

相关文章:

android - 使用 Google Play 服务通过 OAuth2 向 App Engine 进行身份验证

android - OpenGL Point Sprites 在 Android 中工作吗?

android - 如何禁用我的 Android 应用 WebView 字体的设备字体大小更改

c# - 从 Xamarin Forms 元素共享代码访问自定义渲染器实例

c# - 检查是否在 Xamarin.forms 中完成登录

java - 从 xamarin java 绑定(bind)中删除 virtual 关键字

android - 使用 Android 服务的音频逐帧动画

ios - NetworkInterface.GetAllNetworkInterfaces() 返回 Xamarin.iOS 中 OperationalStatus 为 'Unknown' 的接口(interface)

android - 没有名称为 getExternalFilesDirs 的方法 - 三星平板电脑

xamarin - 如何使用MvxRecyclerView在MvvmCross中实现Recycler View