无法启动意向服务
当app处于forground,background或sleep时,我每隔10分钟编写一次同步数据代码。我写了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(intent);来自weakfulintentservice执行完美 但AppService无法启动在xamarin.android中。 请帮我解决这个问题。
答案 0 :(得分:0)
对AppService的调用context.StartService(intent);来自weakfulintentservice执行完美,但AppService无法启动在xamarin.android。
您可以在您想要启动AppService
的Xamarin.Android代码中参考Started Services,您可以像这样简单地编码:
StartService (new Intent (this, typeof(AppService)));
如果您想使用意图过滤器启动服务,可以参考this part。
你也可以参考几天前我回答的案例:Xamarin Android : Change UI TextView text from Service or Receiver。