我試圖在打出電話時顯示通知,但它不起作用(不顯示任何內容)。我單獨測試了通知,它工作正常,但我很確定我在BroadcastReceiver中做了錯誤。 在此先感謝!Xamarin/Android應用程序:廣播接收器無法正常工作
MainActivity:
[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"));
BroadcastReciever類:
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);
}
}
}
你有沒有添加權限處理外撥電話? ('PROCESS_OUTGOING_CALLS') – SushiHangover