【问题标题】:Xamarin Android SetAndAllowWhileIdel RepeatingXamarin Android SetAndAllowWhileIdel 重复
【发布时间】:2016-08-12 14:44:38
【问题描述】:

在 Android Marshmallow 服务或后台进程被暂停/休眠,一种方法是设置警报管理器计时器以每 09 分钟唤醒一次服务。如果我从活动中设置警报,它只会被调用一次。如何设置重复的SetAndAllowWhileIdle 警报?谢谢

 AlarmManager manager = (AlarmManager)GetSystemService(AlarmService);
 long triggerAtTime = SystemClock.ElapsedRealtime() + (9 * 60 * 1000);
 Intent alarmintent = new Intent(this, typeof(AlarmReceiver));
 PendingIntent pendingintent = PendingIntent.GetBroadcast(this, 0, alarmintent, 0);
 if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.M)
 {
      manager.Cancel(pendingintent);
      manager.SetAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup, triggerAtTime, pendingintent);   
 }
 else
 {
      manager.Set(AlarmType.ElapsedRealtimeWakeup, triggerAtTime, pendingintent);
 }

【问题讨论】:

标签: android xamarin alarmmanager


【解决方案1】:

创建一个粘性服务,您可以在其中定义您的 AlarmManager。该服务将由另一个 AlarmManager 调用,警报将在 9 分钟后重新安排。

【讨论】:

    【解决方案2】:

    我做过类似的事情。它似乎有效,但它是正确的方法吗?这是一种递归调用,会泄漏内存。

    服务

    namespace MyApp
    {
        [Service(Name = "com.MyApp.simpleservice", Enabled = true, Process = ":bgservice") ]
        public class SimpleService : Service
        {
            private const string TAG = "MyApp.simpleservice";
    
    
            public override void OnCreate()
            {
                base.OnCreate();
    
            }
    
    
            public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
            {
    
    
                AlarmManager manager = (AlarmManager)GetSystemService(AlarmService);
                long triggerAtTime = SystemClock.ElapsedRealtime() + (5 * 60 * 1000);
                Intent alarmintent = new Intent(this, typeof(AlarmReceiver));
                PendingIntent pendingintent = PendingIntent.GetBroadcast(this, 0, alarmintent, 0);
                if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.M)
                {
    
                    manager.Cancel(pendingintent);
                    manager.SetAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup, triggerAtTime, pendingintent);
                }
                else
                {
                    manager.Set(AlarmType.ElapsedRealtimeWakeup, triggerAtTime, pendingintent);
                }
    
            DoWork();
    
                return StartCommandResult.Sticky;
            }
    
         public void DoWork()
         {
            //Do Some Serious Work here
         }
         public override IBinder OnBind(Intent intent)
             {
                // This example isn't of a bound service, so we just return NULL.
                return null;
            }
    }
    

    广播接收器

    namespace MyApp
    {
        [BroadcastReceiver(Enabled = true) ]
        class AlarmReceiver : BroadcastReceiver
    
        {
            public override void OnReceive(Context context, Intent intent)
            {
                Intent rintent = new Intent(context, typeof(SimpleService));
    
                rintent.PutExtra("AlarmReceiver","Broadcast Alarm");
                context.StartService(rintent);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-23
      • 1970-01-01
      相关资源
      最近更新 更多