2017-09-28 44 views
1

意圖服務無法啓動意圖服務無法啓動在xamarin.android

我寫同步數據的代碼爲每10分鐘whenapp是forground,背景或睡眠。我寫了weakfulintentservice從Alaramreceiver調用它,但AppService沒有得到調用。

AppService服務

public class AppService : WakefulIntentService 
{ 
    public AppService() : base("AppService") 
    { 

    } 

    protected override void DoWakefulWork(Intent intent) 
    { 
     Toast.MakeText(this, "In service", ToastLength.Short).Show(); 
     Log.Info("AppService", "I'm awake! I'm awake!"); 
    } 
} 

WeakFulIntentService

abstract public class WakefulIntentService : IntentService 
{ 
    abstract protected void DoWakefulWork(Intent intent); 
    public static string NAME = "com.jondouglas.wakeful.WakefulIntentService"; 
    public static string LAST_ALARM = "lastAlarm"; 
    private static volatile PowerManager.WakeLock lockStatic = null; 

    [MethodImpl(MethodImplOptions.Synchronized)] 
    private static PowerManager.WakeLock GetLock(Context context) 
    { 
     if (lockStatic == null) 
     { 
      PowerManager manager = (PowerManager) context.GetSystemService(Context.PowerService); 

      lockStatic = manager.NewWakeLock(WakeLockFlags.Partial, NAME); 
      lockStatic.SetReferenceCounted(true); 
     } 
     return (lockStatic); 
    } 

    public static void SendWakefulWork(Context context, Intent intent) 
    { 
     GetLock(context.ApplicationContext); //Possibly use of acquire here 
     context.StartService(intent); 
    } 

    public static void SendWakefulWork(Context context, Type classService) 
    { 
     SendWakefulWork(context, new Intent(context, classService)); 
    } 

    public static void ScheduleAlarms(IAlarmListener alarmListener, Context context) 
    { 
     ScheduleAlarms(alarmListener, context, true); 
    } 

    public static void ScheduleAlarms(IAlarmListener alarmListener, Context context, bool force) 
    { 
     ISharedPreferences preferences = context.GetSharedPreferences(NAME, 0); 
     long lastAlarm = preferences.GetLong(LAST_ALARM, 0); 

     if (lastAlarm == 0 || force || 
      (DateTime.Now.Millisecond > lastAlarm && 
      DateTime.Now.Millisecond - lastAlarm > alarmListener.GetMaxAge())) 
     { 
      AlarmManager manager = (AlarmManager) context.GetSystemService(Context.AlarmService); 
      Intent intent = new Intent(context, typeof(AlarmReceiver)); 
      PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, intent, 0); 
      alarmListener.ScheduleAlarms(manager, pendingIntent, context); 
     } 
    } 

    public static void CancelAlarms(Context context) 
    { 
     AlarmManager manager = (AlarmManager) context.GetSystemService(Context.AlarmService); 
     Intent intent = new Intent(context, typeof (AlarmReceiver)); 
     PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, intent, 0); 
     manager.Cancel(pendingIntent); 
     context.GetSharedPreferences(NAME, 0).Edit().Remove(LAST_ALARM).Commit(); 
    } 

    public WakefulIntentService(string name) : base(name) 
    { 
     SetIntentRedelivery(true); 
    } 

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) 
    { 
     PowerManager.WakeLock wakeLock = GetLock(this.ApplicationContext); 

     if (!lockStatic.IsHeld || (flags & StartCommandFlags.Redelivery) != 0) 
     { 
      wakeLock.Acquire(); 
     } 
     return base.OnStartCommand(intent, flags, startId); 

     return (StartCommandResult.RedeliverIntent); 
    } 

    protected override void OnHandleIntent(Intent intent) 
    { 
     try 
     { 
      DoWakefulWork(intent); 
     } 
     finally 
     { 
      PowerManager.WakeLock wakeLock = GetLock(this.ApplicationContext); 

      if (wakeLock.IsHeld) 
      { 
       try 
       { 
        wakeLock.Release(); 
       } 
       catch (Exception ex) 
       { 
        Log.Error(Class.SimpleName, "Exception when releasing wakelock", ex); 
        //Log exception when releasing wakelock 
       } 
      } 
     } 
    } 

    public interface IAlarmListener 
    { 
     void ScheduleAlarms(AlarmManager manager, PendingIntent pendingIntent, Context context); 
     void SendWakefulWork(Context context); 
     long GetMaxAge(); 
    } 

CallToAppService

 public void SendWakefulWork(Context context) 
    { 
     WakefulIntentService.SendWakefulWork(context, typeof(AppService)); 
    } 

呼叫爲AppService服務context.StartService(意向);從weakfulintentservice執行完美 但AppService無法啓動在xamarin.android。 請幫我解決這個問題。

+0

它與使用java的原生Android完美配合。 –

回答

0

對AppService的調用context.StartService(intent);從weakfulintentservice完美執行,但AppService無法啓動在xamarin.android。

你可以參考Started Services,在你Xamarin.Android代碼要啓動AppService,你可以簡單地編寫就象這樣:如果你想開始一個服務

StartService (new Intent (this, typeof(AppService))); 

意圖過濾器,你可以參考this part

你也可以參考我前幾天回答的案例:Xamarin Android : Change UI TextView text from Service or Receiver

+0

謝謝。但是這並不能解決我的問題。 –

+0

@SwapnilWakchaure,你在AppService的頂部添加了屬性「[Service]」嗎?對於這兩種服務都是 –

+0

。 –