我得到以下例外:
Java.Lang.IllegalStateException:指定的子级已有父级。你必须在孩子的父拳上调用removeVeiw()。
运行以下代码块时。这一切都从我的表单页面开始,按下按钮,使用警报管理器生成预定通知。问题是,如果点击通知,如果应用程序仍处于活动状态,则会抛出上述异常。
如果我在手机上切换应用程序,然后单击通知,它会使应用程序按预期重新启动而不会发生崩溃。
以下是此工作流程的相关代码段:
来自xamarin页面:
private void BtnStartJob_Clicked(object sender, EventArgs e)
{
lblStatus.Text = "";
if (btnStartJob.Text == "Start Job")
{
tmrToggle.StartCommand.Execute(null);
App.Manager.StartJob(App.Manager.currentTimesheet.ProjectID);
btnStartJob.Text = "Start Break";
}
else if (btnStartJob.Text == "End Break")
{
tmrAlert.PauseCommand.Execute(null);
tmrToggle.StartCommand.Execute(null);
App.Manager.EndBreak(App.Manager.currentTimesheet.TimesheetID);
btnStartJob.Text = "Start Break";
var notificationService = DependencyService.Get<INotificationService>();
notificationService.CancelNotification();
}
else if (btnStartJob.Text == "Start Break")
{
tmrAlert.StartCommand.Execute(null);
tmrToggle.PauseCommand.Execute(null);
App.Manager.StartBreak(App.Manager.currentTimesheet.TimesheetID);
btnStartJob.Text = "End Break";
// schedule the notification here.
var notificationService = DependencyService.Get<INotificationService>();
notificationService.CreateNotification("Take Action", "Your break started 1 second ago, please take action.", TimeSpan.FromSeconds(15).Ticks);
}
}
NotificationService
public class NotificationService : INotificationService
{
public void CancelNotification()
{
var alarmIntent = new Intent(Android.App.Application.Context, typeof(AlarmReceiver));
var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = Android.App.Application.Context.GetSystemService("alarm").JavaCast<AlarmManager>();
alarmManager.Cancel(pending);
}
public void CreateNotification(string title, string message, long durationInTicks)
{
var duration = TimeSpan.FromTicks(durationInTicks);
var alarmIntent = new Intent(Forms.Context, typeof(AlarmReceiver));
alarmIntent.PutExtra("title", title);
alarmIntent.PutExtra("message", message);
var pending = PendingIntent.GetBroadcast(Android.App.Application.Context, 0, alarmIntent, PendingIntentFlags.UpdateCurrent);
var alarmManager = Android.App.Application.Context.GetSystemService("alarm").JavaCast<AlarmManager>();
alarmManager.Set(AlarmType.ElapsedRealtime, duration.Milliseconds, pending);
}
}
警报接收器:
[BroadcastReceiver]
class AlarmReceiver : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
var message = intent.GetStringExtra("message");
var title = intent.GetStringExtra("title");
var resultIntent = new Intent(context, typeof(MainActivity));
resultIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);
var pending = PendingIntent.GetActivity(context, 0,
resultIntent,
PendingIntentFlags.CancelCurrent);
var builder =
new Notification.Builder(context)
.SetContentTitle(title)
.SetContentText(message)
.SetSmallIcon(Resource.Drawable.WESSUClogo)
.SetDefaults(NotificationDefaults.All);
builder.SetContentIntent(pending);
var notification = builder.Build();
var manager = NotificationManager.FromContext(context);
manager.Notify(1337, notification);
}
}
MainActivity: 全球:: Xamarin.Forms.Platform.Android.FormsApplicationActivity {
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
有关如何解决的任何建议?对我来说,最大的问题是我需要在removeView()上调用哪个对象? LoadApplication(newApp())抛出此异常;我的MainActivity中的一行。
由于完整堆栈跟踪非常长,因此请单击here以获取pastebin。
答案 0 :(得分:0)
我自己发现了答案。
在AlarmReceiver里面,我需要做的就是:resultIntent.SetFlags(ActivityFlags.Singletop);