gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:10:54 28 4
gpt4 key购买 nike

我试图在拨出电话时显示通知,但它不起作用(未显示任何内容)。我单独测试了通知,它工作正常,但我很确定我在 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com