无法在Xamarin.forms中使用AlaramManager启动Android服务

时间:2017-09-20 10:10:57

标签: android xamarin.forms xamarin.android

使用DependencyService调用StartBackgroundSynchService。

AlaramManager代码

    public void StartBackgroundSynchService()
    {
        try
        {
            AlarmManager am = (AlarmManager)Forms.Context.GetSystemService(Context.AlarmService);
            Intent intentService = new Intent(Forms.Context, typeof(ReportSynchService));

            PendingIntent pendingIntent = PendingIntent.GetBroadcast(Forms.Context, 0, intentService, 0);
            am.SetRepeating(AlarmType.RtcWakeup, DateTime.Now.Millisecond, 1000, pendingIntent);
        }catch(Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
        }
     }

服务类

public class ReportSynchService : Service
{

    public ReportSynchService()
    {
        System.Diagnostics.Debug.WriteLine("In Services Constructor");
    }
    public override IBinder OnBind(Intent intent)
    {
        System.Diagnostics.Debug.WriteLine("In Services OnBind");
        return null;
    }

    public override void OnCreate()
    {
        base.OnCreate();
        System.Diagnostics.Debug.WriteLine("In Services OnCreate");

    }


    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
    {
        System.Diagnostics.Debug.WriteLine("In Services OnStartCommand");
        return StartCommandResult.NotSticky;
    }

}

Alaram管理器代码正常工作但未调用服务类。 请帮助我解决这个问题。

0 个答案:

没有答案