2016-03-29 33 views
1

Hello, Bir hizmeti başlatabileceğiniz, tam olarak çalışan ve bir bildirim oluşturan bir uygulama oluşturmak istiyorum ve bu hizmet DateTime ise sürekli olarak kanıtlanmalıdır. .Now.Date, spezific Date'den daha büyük.Xamarin Android Arkaplan Hizmeti, uygulama kapandığında çöküyor

Aşağıdaki kodu çalıştırdığımda, bildirim görüntülenir, ancak uygulamayı kapattığımda, birkaç saniye sonra uygulamanın kilitlendiği ve neden bilmediğiyle ilgili iki kez bilgi alırım.

uygulama kapatıldığında bu anly olur çünkü ben bile kod hata ayıklama olamaz ....

Bana teşekkür yardım eder! İşte

benim kodudur:

namespace App 
{ 
    [Activity(Label = "App", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity 
    { 
     int count = 1; 

     protected override void OnCreate(Bundle bundle) 
     { 
      base.OnCreate(bundle); 

      // Set our view from the "main" layout resource 
      SetContentView(Resource.Layout.Main); 

      // Get our button from the layout resource, 
      // and attach an event to it 
      Button button = FindViewById<Button>(Resource.Id.MyButton); 

      button.Click += delegate { 
       button.Text = string.Format("{0} clicks!", count++); 
       StartService(new Intent(this, typeof(backgroudservice))); 
      }; 
     } 
    } 

    public class backgroudservice : Service 
    { 
     public override IBinder OnBind(Intent intent) 
     { 
      return null; 
     } 

     public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId) 
     { 

      newnotification("Title", "Text: ", 0); 

      new Task(() => { 
       DoWork(); 
       Thread.Sleep(1000); 
      }).Start(); 

      return StartCommandResult.Sticky; 
     } 

     public void DoWork() 
     { 
      if (DateTime.Now.Date > Convert.ToDateTime("2016-03-29").Date) 
      { 
       cancelnotification(0); 
       StopSelf(); 
      } 
     } 

     public override void OnDestroy() 
     { 
      base.OnDestroy(); 
      cancelnotification(0); 
     } 

     private void newnotification(string titel, string text, int id) 
     { 
      Notification.Builder builder = new Notification.Builder(this) 
      .SetContentTitle(titel) 
      .SetContentText(text) 
      .SetSmallIcon(Resource.Drawable.droidlogo_small) 
      .SetAutoCancel(false) 
      .SetVisibility(NotificationVisibility.Public) 
      .SetContentIntent(PendingIntent.GetActivity(this, 0, new Intent(this, typeof(MainActivity)), PendingIntentFlags.OneShot)); 

      // Build the notification: 
      Notification notification = builder.Build(); 
      notification.Flags = NotificationFlags.NoClear; 
      //notification.ContentIntent = new Intent(this,typeof(login)); 
      // Get the notification manager: 
      NotificationManager notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; 

      // Publish the notification: 
      notificationManager.Notify(id, notification); 

     } 
     private void cancelnotification(int id) 
     { 
      NotificationManager notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; 
      notificationManager.Cancel(id); 
     } 
    } 
} 
+0

Uygulamanızı öldürmek için Görev Yöneticisi'ni kullanıyorsanız. Muhtemelen Hizmeti de kesiyorsunuz. –

cevap

0

Baz yöntemine aramadan önce için hizmet en OnDestroy yılında cancelnotification çağrısını hareketli deneyebilirsiniz, yani:

public override void OnDestroy() 
    { 
     cancelnotification(0); 
     base.OnDestroy(); 
    } 
0

Ben çözüldü Bu, dersimin üstünde [Hizmet] 'i unuttum, şimdi işe yarıyor!

[Service] 
    public class backgroudservice : Service 
    { 
    ... 
    } 
+0

Adam da ben aynı sorunu var, yaptım tüm wat yaptım, eğer manifestini paylaşırsan lütfen –

İlgili konular