私は次の例外を取得指定された子は、すでに親を持っています。子の親の拳でremoveVeiw()を呼び出す必要があります。Xamarin通知Androidのクラッシュは
次のコードブロックを実行しているとき。フォームマネージャでは、アラームマネージャを使用してスケジュール通知を生成するボタンプレスを使用してフォームページから開始します。問題は、通知がクリックされると、アプリがまだアクティブな場合は上記の例外がスローされるということです。
電話機のアプリケーションを切り替えてから通知をクリックすると、クラッシュせずに意図したとおりにアプリケーションがバックアップされます。
以下は、このワークフローの問題のコードスニペットです: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のライン。
完全なスタックトレースは実際には長いので、pastebinの場合はhereをクリックしてください。
完全なスタックトレースを提供する可能性はありますか? – pushasha
あなたはあなたのAlarmReceiverを知らなかった - あなたはNotificationServiceを2回投稿した。そして、あなたが例外を引き起こしている特定の行を教えてもらうと助けになるでしょう – Jason
スタックトレース – Nebri